4

If I have an array like this:

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(2) "34"
    ["total"]=>
    string(6) "122337"
  },
[1]=>
  array(2) {
    ["id"]=>
    string(2) "43"
    ["total"]=>
    string(6) "232337"
  }
}

And I want to add a new key value to each sub array, so for example, it would end like this:

array(2) {
      [0]=>
      array(2) {
        ["id"]=>
        string(2) "34"
        ["total"]=>
        string(6) "122337"
        ["newkey"]=>
        string(6) "hihihi"
      },
    [1]=>
      array(2) {
        ["id"]=>
        string(2) "43"
        ["total"]=>
        string(6) "232337"
       ["newkey"]=>
        string(6) "hihihi"
      }
    }

How would I do it?

I have tried with a foreach like this:

foreach($exterior_array as $inside_array) {
 $inside_array['newkey'] = "hihihi";
}

But once I get inside the foreach, the values are not saved.

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296

8 Answers8

6
foreach($exterior_array as $inside_array) {
    $inside_array['newkey'] = "hihihi";
}

But once I get inside the foreach, the values are not saved.

That is because you are working on a copy of the array via $inside_array. You can access the "orignal" value you want to change by making $inside_array an alias of the origina value; using a reference:

foreach($exterior_array as &$inside_array) {
                           ^- set the reference
    $inside_array['newkey'] = "hihihi";
}
unset($inside_array);
^^^^^^^^^^^^^^^^^^^^^- remove the reference

Compare with http://php.net/foreach

hakre
  • 193,403
  • 52
  • 435
  • 836
0
foreach($exterior_array as $k=>$inside_array) {
 $exterior_array[$k]['newkey'] = "hihihi";
}

try this

Svetoslav
  • 4,686
  • 2
  • 28
  • 43
0

Because you are using it as a temporary array, do it like this:

foreach($exterior_array as $key => $inside_array)
{
    $exterior_array[$key]['newkey'] = "hihihi";
}

Or you can do it with references as jpo suggested, this will make a new array, but keep it linked to the original (note the &):

foreach($exterior_array as &$inside_array)
{ 
    $inside_array['newkey'] = "hihihi";
} 
Community
  • 1
  • 1
Peon
  • 7,902
  • 7
  • 59
  • 100
0
foreach($exterior_array as $key => $inside_array) {
        $inside_array[$key]['newkey'] = "hihihi";
    }
Sibu
  • 4,609
  • 2
  • 26
  • 38
0

Another solution using references:

foreach($exterior_array as &$inside_array) { 
    $inside_array['newkey'] = "hihihi";
} 
Thomas Glaser
  • 1,670
  • 1
  • 18
  • 26
0

Try this. Tested and verified.

<?php
    $parentArray = array(
        array("id"=>1),
        array("id"=>2),
        array("id"=>3),
    );

    foreach($parentArray as $key=>$childArray)
    {
        $parentArray[$key]['newkey'] = "hello";
    }

    //output

    Array
    (
        [0] => Array
            (
                [id] => 1
                [newkey] => hello
            )
        [1] => Array
            (
                [id] => 2
                [newkey] => hello
            )
        [2] => Array
            (
                [id] => 3
                [newkey] => hello
            )
    )
?>
Peon
  • 7,902
  • 7
  • 59
  • 100
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29
0

Its not a perfect fit for this topic, but I used this a lot in my own projects.

http://pastebin.com/TyWzLWuK

Its not very performant but easy to handle.

Example:

Fw_Recursive_Array_Helper::set($array, '0.someKey.someSubKey', 'value');
if(Fw_Recursive_Array_Helper::has($array, '0.someKey.someSubKey')) {
   echo Fw_Recursive_Array_Helper::get($array, '0.someKey.someSubKey'); 
}
echo Fw_Recursive_Array_Helper::get($array, '1.someKey.someSubKey', 'If the key does not exist, use this');
Ron
  • 1,336
  • 12
  • 20
0
class helper
{

public function arrayInsert($key=NULL,$value=NULL,& $array=array())
{

if(!empty($key)&&!empty($value)&&is_array($array))
{
 $array[$key]=$value;
}
}
}

$obj=new helper();


$array=array('1'=>1,'a'=>'a');
$obj->arrayInsert('b','b',$array);
print_r($array)

o/p=>Array ( [1] => 1 [a] => a [b] => b )

Arun Killu
  • 13,581
  • 5
  • 34
  • 61