hpg4815 Posted January 27, 2009 Share Posted January 27, 2009 What am i missing? <? $array = array("apple", "cherry"); $a = array_unshift($array, "banna", "kiwi"); print_r($a); ?> only results in print_r displaying the number 4? I don't see anyone having the same problem as I, so I can only assume that I have done something wrong. Quote Link to comment https://forums.phpfreaks.com/topic/142634-array_unshift-doesnt-work-for-me/ Share on other sites More sharing options...
rhodesa Posted January 27, 2009 Share Posted January 27, 2009 you are using it wrong: <?php $array = array("apple", "cherry"); array_unshift($array, "banna", "kiwi"); print_r($array); ?> it modifies the array passed. the return value is the new number of elements in the array Quote Link to comment https://forums.phpfreaks.com/topic/142634-array_unshift-doesnt-work-for-me/#findComment-747608 Share on other sites More sharing options...
hpg4815 Posted January 27, 2009 Author Share Posted January 27, 2009 OK. The example here states the same, but it also states the output to be the array. http://us3.php.net/manual/en/function.array-unshift.php Which function simply prepends an element on the front of an existing array? What good is Returning the new number of elements in the array? Couldn't I get that info from the sizeof() function? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142634-array_unshift-doesnt-work-for-me/#findComment-747771 Share on other sites More sharing options...
premiso Posted January 27, 2009 Share Posted January 27, 2009 Which function simply prepends an element on the front of an existing array? What good is Returning the new number of elements in the array? Couldn't I get that info from the sizeof() function? array_unshift The return value of array_unshift Yes you could get it from sizeof, but why? There is no need to when it is being returned like that. <edit> The good it does to return that instead of sizeof to know that your elements were added. You can do a check to verify. The array is modified on it's own since it is essentially passed by reference, thus there is no need to return the array cause it is modified without doing that. And the manual does not state the output to be an array: int array_unshift ( array &$array , mixed $var [, mixed $... ] ) Int is not an array, it is a number. </edit> <?php $array = array("apple", "cherry"); $newcount = array_unshift($array, "banna", "kiwi"); echo "The new array count is: $newcount <br /><br />"; print_r($array); ?> Should print 4 then the array. Quote Link to comment https://forums.phpfreaks.com/topic/142634-array_unshift-doesnt-work-for-me/#findComment-747785 Share on other sites More sharing options...
hpg4815 Posted January 27, 2009 Author Share Posted January 27, 2009 Thank you. I understand now. Quote Link to comment https://forums.phpfreaks.com/topic/142634-array_unshift-doesnt-work-for-me/#findComment-747808 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.