5

i am in need of bit direction here for renaming my inner child keys who inner depth is unknown. Original array is parent child multidimensional array created via id and parent_id relation. Here's a one example of array


$testArray = array (
 "name" => "Test name",
 "someValue1" => 834.69,
 "someMoreValue" => 33.4,
 "someCode" => 6668,
 "child" => array
 (
 array
 (
 "name" => "Some name",
 "someValue1" => 471.05,
 "someMoreValue" => 18.84,
 "someCode" => 7064,
 "child" => array
 (
 array
 (
 "name" => "Yet another name",
 "someValue1" => 438.62,
 "someMoreValue" => 17.56,
 "someCode" => 7065
 ),
 array
 (
 "name" => "Da name",
 "someValue1" => 4444,
 "someMoreValue" => 44,
 "someCode" => 7044
 )
 )
 ),
 array
 (
 "name" => "name",
 "someValue1" => 2222,
 "someMoreValue" => 22,
 "someCode" => 7022
 )
 )
);

here's my trying so far, i am able to rename the first level of childs only.

function keyprefix($keyprefix, $keyprefix2, Array $array) {
 foreach($array as $k=>$v){
 $array[$keyprefix.'-'.$k.'-'.$keyprefix2] = $v;
 unset($array[$k]);
 }
 return $array; 
}
function test($array) {
 $newArr = array();
 foreach ($array as $key => $value) {
 // $newArr[] = is_array($value) ? test($value) : $value;
 // $newArr[] = is_array($value) ? $array : array_merge( keyprefix("$name","Alt Danışman", $array[$key]) );
 $index = 0;
 $name = $array['name'];
 if (is_array($value)) {
 //if (is_array($value)) {
 // test($value);
 //}
 $newArr[$key] = array_merge(
 keyprefix("$name","Under Child", $array[$key])
 );
 } 
 else {
 $newArr[$key] = $value;
 }
 }
 return $newArr;
}
echo highlight_string(var_export(test($testArray), true));

enter image description here

Any ideas on how I can approach this? I feel that I am close just need a little guide in the right path. I have seen many examples of stack-overflow etc but need some help. I know recursion is not my best skill yet.

asked Feb 11, 2019 at 2:28
4
  • What do you want to rename them too? Something based on the depth, or the parent name, or ??? Commented Feb 11, 2019 at 2:32
  • hey @Nick i added pic of my code, as you can see i renamed them based on there respective parent name followed by some text. the keyprefix is actually renaming all keys of provided array Commented Feb 11, 2019 at 2:35
  • Rename array with key child only or rename every inner array? Commented Feb 11, 2019 at 3:27
  • Only key name child have inner arrays @shingo Commented Feb 11, 2019 at 3:30

1 Answer 1

4
function TryRenameChildren(array $array)
{
 if(isset($array['child']))
 {
 $array['child'] = keyprefix($array['name'], 'prefix2', $array['child']);
 foreach($array['child'] as $key => $value)
 if(is_array($value))
 $array['child'][$key] = TryRenameChildren($value);
 }
 return $array;
}
$testArray = TryRenameChildren($testArray);
answered Feb 11, 2019 at 3:39

1 Comment

daymm! thank you @shingo i don't get it, when i will be able to understand recursion in depth. sometimes i just suck :(

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.