15

I have a pretty basic question but I am stuck. I am pretty new to php and I have an array like this:

$array = array(
    'one' => 1,
    'two' => array('key1' => 'val1','key2' => 'val2'),
    'three' => array('key1' => 'val1','key2' => 'val2'),
    'four' => array('key1' => 'val1','key2' => 'val2')
);

and for each of the arrays in the array (that is, 'two, 'three', and 'four'), I want to insert 'key3' => 'val3' into those arrays.

I tried this:

foreach($array as $item) {
    if (gettype($item) == "array") {
        $item['key3'] = 'val3';
    }
}

But it doesn't work, and I'm not sure why. Using various print_r's all over the place, it seems to insert 'key3' => 'val3' into $item if I print it out in the loop, but the original array seems unchanged. I also tried a regular for loop but that didn't work either.

2
  • Do the key and its value are equals for each sub-array? Commented May 10, 2013 at 22:58
  • curious why you didn't just do foreach ($array as $item_key => $item) { $array[$item_key]['key3'] = 'val3'; } Commented Apr 2, 2015 at 2:49

3 Answers 3

24

foreach works with a copy of $item, so you cannot modify your original array inside the foreach. One way to work around this is to use the & operator.

foreach($array as &$item) {
    if (is_array($item)) {
        $item['key3'] = 'val3';
    }
}

Another, more elegant way would be to use array_walk():

array_walk($array, function (&$v, $k) { 
    if (is_array($v)) {
        $v['key3'] = 'val3';
    }
});

This example will work from PHP 5.3, where Closures were introduced.

Sign up to request clarification or add additional context in comments.

6 Comments

$item is only generated for the loop . You mean &$array['key3'] = 'val3'; !
@moskito-x I certainly mean what I wrote. That's the point of it. Check the foreach manual page I linked.
To change $item is useless. Will be always overwritten by the next loop.
But As foreach relies on the internal array pointer changing it within the loop may lead to unexpected behavior. therefore you have to unset the reference . unset($item); // break the reference with the last element. structures.foreach
Thanks for the great answer. The first solution worked fine but the second solution didn't seem to work, it's obvious I need to get a little better at php. Can I just stick that & symbol anywhere? Wow that would change things. Also, thanks for the tip on using unset($item), not knowing that would have eventually sunk me somewhere down the line for sure!
|
0

PHP has a function to check whether a variable is an array or not: is_array(). Use this:

if (is_array($item)) { ...

Comments

0

while looping with foreach use key like :

foreach($array as $key => $item){

    $array[$key]['newElement'] = "newValue";

}

1 Comment

While not the answer to the question, this helped me better understand the relationship between the $key word and the $item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.