Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/142634-array_unshift-doesnt-work-for-me/
Share on other sites

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.