0

I'm trying to change all the values in a recursive array that will have an unknown number/depth of nested arrays. I think it's just syntax that I am tripping over.

Basically I need to output the $orgarray again with all new values.

$orgarray = array( '101' => 'some-value', '102' => 'some-value', '103' => 'some-value', '104' => array( '201' => 'some-value', '202' => 'some-value', '203' => array( '301' => 'some-value', '302' => array( '401' => 'some-value', '402' => 'some-value', '501' => array( '502' => 'some-value', '503' => 'some-value', '504' => 'some-value', '505' => 'some-value', '506' => 'some-vaslue' ), ), ), ), '105' => 'some-value', '106' => 'some-value', '107' => 'some-value' ); function recursearray($array, &$modarray){ foreach($array as $key => $value){ if (is_array($value)){ recursearray($value); // append keys to this nested array ??? }else{ // change current key's value ??? } } } recursearray($orgarray, $modarray); echo '<pre>'; print_r($modarray); echo '</pre>'; 

what am I doing wrong here?

  1. I am unable to change the value of the current key

  2. this won't output an array at all

EDIT ok - I changed the way the function was being called:

function recursearray($array, &$modarray){ if(!isset($modarray)) { $modarray = array(); } foreach($array as $key => $value){ if (is_array($value)){ recursearray($value, &$modarray); // append keys to this nested array // neither of these work array_push($value['newkey'] = 'new_value'); $value['newkey'] = 'new_value'; }else{ // change current key's value $array[$key] = 'value'; } } return $array; } $modarray = recursearray($orgarray, $modarray); 

and now it's almost there, but I still don't understand why the original call to the function did not work ( recursearray($orgarray, $modarray); ) and the 2 methods trying to add keys to the nested arrays do not work either.

9
  • @Jack Are you not able to see the snippet of code he provided? Commented Feb 16, 2013 at 3:01
  • 2
    You want to change what how? What result do you expect? Commented Feb 16, 2013 at 3:01
  • @AustinBrunkhorst - What snippet of code? OP only provided a printout of an array. Commented Feb 16, 2013 at 3:03
  • @felix, basically I have an API function from a CMS that outputs document ids in a handy array who's structure matched the site structure, but all it returns are the ids. I need to look up each id, get some information about it [doc name, uri etc] and update the org array with that info. Commented Feb 16, 2013 at 3:05
  • 1
    All you need is $array[$key] = $new_value;. Commented Feb 16, 2013 at 3:24

1 Answer 1

1

I think this will works:

<?php function recursearray(&$array){ foreach($array as $key => &$value){ if (is_array($value)){ recursearray($value); }else{ $value = 'other-value'; }; }; }; ?> 

Take atention to foreach($array as $key => &$value){

Regards!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.