array_unshift(): Adding one element at starting of the array
![]() To add one or more element at the beginning of the array. After applying array_unshift() total elements of the array increases by one or more . All numerical array keys are re-indexed but literal keys remain same.
Returns the total number of elements present (after adding using array_unshift() to the array.) Here $input is an array, $output get the value of total number of elements of the array.
Output of above code is here( $output gets the number of elements present 6 )
To add more than one element we will change the code like this.
With Literal Keys
Output is here, ( $output gets the first element Banana)
Now key of the first element is 0. To add one element with key to an associative array we have to use the code below.
Output is here
Using currentBy using current() we can get the element to which the present internal pointer is pointing. By defalut it points to first element of the array.
Output is here.( The first element is changed from Banana to Mango)
Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
![]() |
|