Jump to content
Killersites Forums

HOW TO: array_push / array_unshift with string keys


jtified
 Share

Recommended Posts

Hi guys!

 

How can I insert and array with string index or string key. Like for example

 

<?php

 

$example['breakfast'] = 'ham and egg';

$example['lunch'] = 'steak';

$example['snack'] = 'fish and chips';

 

print_r ($example);

 

?>

 

this outputs:

 

Array ( [breakfast] => ham and egg [lunch] => steak [snack] => fish and chips )

 

 

If I wanted to use array_push() or array_unshift(), HOW can I insert an item in the array "$example" that has a string index or string key of ['dinner']. Something like this:

 

<?php

 

$example['breakfast'] = 'ham and egg';

$example['lunch'] = 'steak';

$example['snack'] = 'fish and chips';

 

array_push($example['dinner'] = 'pizza');

print_r ($example);

 

?>

 

It generates the output :

 

Warning: array_push() expects at least 2 parameters, 1 given in C:\wamp\www\SMS 2.0\sample.php on line 16

Array ( [breakfast] => ham and egg [lunch] => steak [snack] => fish and chips [dinner] => pizza )

 

But you see the dinner was inserted..but how do I remove the error.

Link to comment
Share on other sites

Warning: array_push() expects at least 2 parameters, 1 given in C:\wamp\www\SMS 2.0\sample.php on line 16

Array ( [breakfast] => ham and egg [lunch] => steak [snack] => fish and chips [dinner] => pizza )

 

But you see the dinner was inserted..but how do I remove the error.

 

Just learning myself, but if the warning is asking for two parameters, maybe by just adding a comma after $example will work. eg. [array_push($example, ['dinner'] = 'pizza');].

 

If that doesn't work couldn't you just use $example['dinner'] = 'pizza';

Edited by dms
Link to comment
Share on other sites

When you have an error like this, the first thing you need to do is check the PHP manual for that particular function:

http://us.php.net/array_push. If you still can't get it to work, then a google search for the issue usually works (in this case, I looked up "array_push php associative array").

 

I was able to get your last example to work using PHP4... but it generates an error that I can't seem to fix when I switch to PHP5. The array_push() function requires two parameters -- the array, and the value. In PHP5, it looks like you can't input "$example['dinner']" as the array, because that refers to a specific key, not the entire array.

 

Based on this (http://devzone.zend.com/article/635 -- Under the "Push and Pull" section) it looks like you can't use array_push() with associative arrays. "The array_push() and array_unshift() functions don't work with associative arrays; to add elements to these arrays, it's better to use the $arr[$key] = $value notation to add new values to the array." (this is also faster than calling a function just to add one value to an array.)

 

So, to add a new value to an associative array, just use:

$example['dinner'] = 'pizza';

Link to comment
Share on other sites

So we can insert additional items in an assoc. array in the end of the array but not in the beginning of it.

As far as I can tell, yes.

 

I don't see how "beginning" or "end" of an associative array make any difference though. It only makes a difference when you print out the array using print_r, but I can't see you doing that often in a project that is going to be visible to a general audience. I can understand if you are relying on the order of an array (like a non-associative array $array[1], $array[2] etc) but that's the whole point of an associative array -- order isn't important, and you will still find all of the data easily because of the key you assigned each value in the array.

Link to comment
Share on other sites

There may be absolutely no use for this, but if you ever do need to insert an item at the beg/end of an array; you could give the key a name with a low sort value, such as... ['1_dinner'] or a high sort value... ['zzz_dinner'], and then use the ksort() along with array_flip(). The only problem I see is that if you have different keys with the same value, you will lose all but the last key/value pair.

e.g.

<?php

$example['breakfast'] = 'ham and egg';

$example['lunch'] = 'steak';

$example['snack'] = 'fish and chips';

 

$example['1_dinner'] = 'pizza';

 

ksort($example);

foreach ($example as $key => $val) {

echo "$key = $val\n";

}

 

$example = array_flip($example);

$example['pizza'] = 'dinner';

$example = array_flip($example);

print_r($example);

?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...