Warning when using Array_Push

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Warning when using Array_Push

Post 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.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Warning when using Array_Push

Post 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.
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Warning when using Array_Push

Post 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.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Warning when using Array_Push

Post 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 :)
Post Reply