[ create a new paste ] login | about

Link: http://codepad.org/LdQEGcBz    [ raw code | output | fork ]

PHP, pasted on Nov 12:
<?php

function &getArrayPath(&$arr,$path) {
    foreach($path as $item){
        $arr =& $arr[$item];
    }
    return $arr;
}


$map = json_decode('[{"child":"1"},{"child":"2"},{"child":"3"}]', true);
$path = array("0","child");

$target =& getArrayPath($map,$path);

var_dump($map);

print "\nchange\n\n";

$target = "new";

var_dump($map);


Output:
array(3) {
  [0]=>
  array(1) {
    ["child"]=>
    &string(1) "1"
  }
  [1]=>
  array(1) {
    ["child"]=>
    string(1) "2"
  }
  [2]=>
  array(1) {
    ["child"]=>
    string(1) "3"
  }
}

change

array(3) {
  [0]=>
  array(1) {
    ["child"]=>
    &string(3) "new"
  }
  [1]=>
  array(1) {
    ["child"]=>
    string(1) "2"
  }
  [2]=>
  array(1) {
    ["child"]=>
    string(1) "3"
  }
}


Create a new paste based on this one


Comments: