Page 1 of 1

Warning when using Array_Push

Posted: Fri Aug 22, 2008 11:57 am
by The_Anomaly
Hey guys,


Well, I googled this a bit, and I understand what normally would cause this warning--but I don't see how it applies in my situation. Here's my code:

Code: Select all

 
$random = rand();
array_push($post['random'],$random);
$post['random'] = $random;
I get this warning:

Code: Select all

Warning: array_push() [function.array-push]: First argument should be an array...
The code works, perfectly. Just that the output of this warning is screwing up the Header--and I want to know how I can do this without PHP thinking it's a mistake.

First off, I am certain that $post is an array. I was trying to push a random value to it, with with the key of "random". Why is this giving me this error, when the first argument is an array?

Thanks.

Re: Warning when using Array_Push

Posted: Fri Aug 22, 2008 12:01 pm
by ghurtado
Basically, your code works because line 3 is the wrong way of doing what you did right in line 4. You dont need to use array_push if you are working with associative arrays.

Re: Warning when using Array_Push

Posted: Fri Aug 22, 2008 12:08 pm
by The_Anomaly
ghurtado wrote:Basically, your code works because line 3 is the wrong way of doing what you did right in line 4. You dont need to use array_push if you are working with associative arrays.
Very sweet. Thank you for the quick response and perfect solution.

Just for clarity's sake, the reason I was doing what I did in line 3 was because of this, in the manual. Clearly, not all comments are entirely correct. I didn't know that you can add values to an array by simply assigning them.

Re: Warning when using Array_Push

Posted: Fri Aug 22, 2008 12:16 pm
by ghurtado
Beware the comments in the manual: they are not moderated at all and they can include anything, from the daft to the dangerous to the flat out wrong, as you just discovered :)