Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am receiving an error once this code runs. I have looked up possible solutions but everything seems to be formatted correctly.

$searched = 'searched';    
$count    = '59';
$prop     = Array();

$i = 0;
while ($i++ <= 4) {
    array_push($prop[$i] = Array(
         'text' => $searched,
         'href' => 'http://mysite.com/?search=' . str_replace(' ', '+', $searched)
    ));
}

array_push($prop['Total Searches'] = $count);

I receive this error 5 times for the while loop, and 1 time for the array_push under the while loop.

Warning: Wrong parameter count for array_push()

The code works correctly! but it still calls out the error. So should I just suppress the error?

share|improve this question
I altered your formatting a bit to make it vaguely legible. Next time, please go ahead and do that yourself! – Lightness Races in Orbit May 23 '11 at 21:51
2  
-1 for considering "just suppress[ing] the error". +1 for realising that it might not be a good idea, and asking about it. :) – Lightness Races in Orbit May 23 '11 at 21:52
Thank you, I couldn't figure out how the back ticks worked. Im going to create an account here since I received an answer so swiftly – Tj tarmon May 23 '11 at 22:00
SO is pretty awesome. – Lightness Races in Orbit May 23 '11 at 22:04
add comment (requires an account with 50 reputation)

3 Answers

up vote 5 down vote accepted

Change:

array_push($prop['Total Searches'] = $count);

to:

$prop['Total Searches'] = $count;

etc.

You only use array_push to push a value on to the end of a list-style array. This is not relevant here, as you're just setting a new key/value pair.

share|improve this answer
Aw, you worded it better. – Lightness Races in Orbit May 23 '11 at 21:52
Thank you! I have been starring down this code for almost an hour. – Tj tarmon May 23 '11 at 21:55
You're welcome. Please click the checkmark outline next to this answer if you are happy with it. That will indicate the question is answered and help other people find the favorite answer. Welcome to SO! – dkamins May 23 '11 at 21:57
Done! I tried clicking it as soon as i ran the code, but I had to wait 11 mins :/ Thanks again – Tj tarmon May 23 '11 at 22:04
add comment (requires an account with 50 reputation)

You're mixing approaches.

Read about array_push, which doesn't do what you think it does.

array_push($array, $val) is like $array[] = $val.

You want just:

$prop[$i] = Array(
     'text' => $searched,
     'href' => 'http://mysite.com/?search=' . str_replace(' ', '+', $searched)
));

and

$prop['Total Searches'] = $count;
share|improve this answer
+1 - dkamins has it right as well, but this is a better answer in my opinion – Eric Petroelje May 23 '11 at 21:54
Thanks, from what dkamins said i was able to remove the array_push from the while loop as you described. – Tj tarmon May 23 '11 at 21:57
@Eric: It's more thorough, but it's certainly not one of my most comprehensible answers. :) – Lightness Races in Orbit May 23 '11 at 22:03
add comment (requires an account with 50 reputation)

this will do your job,

$searched = 'searched';    
$count    = '59';
$prop     = Array();
$search_terms = Array();

$i = 0;
while ($i <= 4) 
{
   $search_terms['text'] = $searched;
   $searched = str_replace(' ', '+', $searched);
   $search_terms['href'] = 'http://mysite.com/?search='.$searched;
   array_push($prop, $search_terms);
   $i++;
}

$prop['Total Searches'] = $count;

and check http_build_query, thats what i am using.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.