Danish Raza
asked on
Update specific value of key in multidimensional array
i have tried some several way of updating specific array at specific depth in multidimensional array. Although i am able to update but it is not modifying the original array. Even though i tried the referencing technique of php but it seems odds are not in my favour.
let's say, the above array is in function A and i passed that above array, which lets say is $tree[$i]['child'], like below:
I was successfully able to find Specific array using
so above example i was targeting the array with clientCode = 06459 and if found i was updating its individualCommission key to some value. if i do dump after updating with $array['individualCommissi on'] = '10000000000000';, it seems, i was able to successfully update it. but unfortunately original array is still same hence unmodified.
Another try i did, it updated that specific array and return that specific array only and i lost all other arrays of multidimensional array.
so i will appreciate if anyone would spare some time and find any solution for this simple problem.
Thanks!
// Multidimensional array would be as tree
// lets call this array = $tree[$i]['child']
Array
(
[0] => Array
(
[name] => hasan
[clientCode] => 06442
[somekey] => 707
[individualCommission] => 10
[child] => Array
(
[0] => Array
(
[name] => Aslan
[clientCode] => 06447
[somekey] => 712
[individualCommission] => 10
)
[1] => Array
(
[name] => Kudret
[clientCode] => 06448
[somekey] => 713
[individualCommission] => 10
)
[2] => Array
(
[name] => Emir
[clientCode] => 06446
[somekey] => 711
[individualCommission] => 10
)
)
)
[1] => Array
(
[name] => emine
[clientCode] => 06443
[somekey] => 708
[individualCommission] => 10
[child] => Array
(
[0] => Array
(
[name] => Kaan
[clientCode] => 06449
[somekey] => 714
[individualCommission] => 10
)
[1] => Array
(
[name] => Duygu
[clientCode] => 06450
[somekey] => 715
[individualCommission] => 10
)
[2] => Array
(
[name] => Hakan
[clientCode] => 06451
[somekey] => 716
[individualCommission] => 10
)
[3] => Array
(
[name] => Yesim
[clientCode] => 06452
[somekey] => 717
[individualCommission] => 10
)
)
)
[2] => Array
(
[name] => Aysel
[clientCode] => 06444
[somekey] => 709
[individualCommission] => 10
[child] => Array
(
[0] => Array
(
[name] => Ismet
[clientCode] => 06453
[somekey] => 718
[individualCommission] => 10
)
[1] => Array
(
[name] => Suna
[clientCode] => 06454
[somekey] => 719
[individualCommission] => 10
)
[2] => Array
(
[name] => Kudriya
[clientCode] => 06455
[somekey] => 720
[individualCommission] => 10
)
)
)
[3] => Array
(
[name] => Ali
[clientCode] => 06445
[somekey] => 710
[individualCommission] => 10
[child] => Array
(
[0] => Array
(
[name] => Gulen
[clientCode] => 06456
[somekey] => 721
[individualCommission] => 10
)
[1] => Array
(
[name] => Halit
[clientCode] => 06457
[somekey] => 722
[individualCommission] => 10
[child] => Array
(
[0] => Array
(
[name] => Seniz
[clientCode] => 06458
[somekey] => 723
[individualCommission] => 10
)
[1] => Array
(
[name] => Güzin
[clientCode] => 06459
[somekey] => 724
[individualCommission] => 10
[child] => Array
(
[0] => Array
(
[name] => test client
[clientCode] => 06460
[somekey] => 725
[individualCommission] => 10
)
[1] => Array
(
[name] => test client 2
[clientCode] => 06461
[somekey] => 726
[individualCommission] => 10
)
)
)
)
)
)
)
)
let's say, the above array is in function A and i passed that above array, which lets say is $tree[$i]['child'], like below:
function A($tree) {
$i = 5;
$childArr = &$tree[$i]['child'];
$childArr = findandReplace($childArr);
dump($childArr); // still same nothing changed at that specific index of array
}
I was successfully able to find Specific array using
function findandReplace($array) {
// Loops through each element. If element again is array, function is recalled. If not, result is echoed.
foreach($array as $key=>$value)
{
if(is_array($value))
{
findandReplace($value);
}
else{
if ($key == 'clientCode' && $value == '06459') {
// echo $key." = ".$value."<br />\n";
$array['individualCommission'] = '10000000000000';
break;
}
}
}
return $array;
}
so above example i was targeting the array with clientCode = 06459 and if found i was updating its individualCommission key to some value. if i do dump after updating with $array['individualCommissi
Another try i did, it updated that specific array and return that specific array only and i lost all other arrays of multidimensional array.
function findandReplace($array) {
$parent = &$array;
$iterator = new \RecursiveIteratorIterator( new \RecursiveArrayIterator($array), \RecursiveIteratorIterator::SELF_FIRST );
// $result = array();
foreach ($iterator as $key => $item) {
if ( $key === 'clientCode' && $item == '06459') {
$parent = (array)$iterator->getInnerIterator();
$parent['individualCommission'] = '1000000000000000';
// $array[3]['child'][1]['child'][1]['individualCommission'] = $parentCommission;
break;
}
}
return $array;
}
so i will appreciate if anyone would spare some time and find any solution for this simple problem.
Thanks!
ASKER
Hey @Julian thank you for your reply. but if you observed closely in my case i am updating particular $array after verifying if key is clientCode and $value matches clientCode as well.
see this
so this is where i am updating "individualCommission" key of that particular array. Although in functionA i did pass this array as refrence.
I tried your comment but i didn't effect anything!
see this
if ($key == 'clientCode' && $value == '06459') {
// echo $key." = ".$value."<br />\n";
$array['individualCommission'] = '10000000000000';
break;
}
so this is where i am updating "individualCommission" key of that particular array. Although in functionA i did pass this array as refrence.
I tried your comment but i didn't effect anything!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi @Julian Hansen yes but if i do not write additional
findandReplace(&$array)
refrence in function params then it doesn't save back!! It seems to me very weird though but i believe it is because of recursion
Sorry but you implemented a change I recommended on line 2 of your latest code - that was the reason your code was not working NOT BECAUSE OF RECURSION
You were calling this
Changing the $value to &$value in the for loop is what was needed to fix the code - which I recommended, you implemented and now it is working.
I don't understand why you are saying you did not get a solution!
You were calling this
findandReplace($value);
With a value parameterChanging the $value to &$value in the for loop is what was needed to fix the code - which I recommended, you implemented and now it is working.
I don't understand why you are saying you did not get a solution!
ASKER
why don't you go ahead and try what you commented about & sign next to $value. you did provide aid to solution for which i am thankful. but indeed your recommendation didn't solve my problem. also it is not the case that i didn't think of putting & sign in foreach. i did try myself many other combinations!
I will admit i just luckily solve it while tracing and putting a & in function findandreplace()
I will admit i just luckily solve it while tracing and putting a & in function findandreplace()
Try calling findandreplace(&$array) with your foreach set to $value - it won't work.
Anyway I wish you well with your project
also it is not the case that i didn't think of putting & sign in foreach. i did try myself many other combinations!You never mentioned this in your OP.
Anyway I wish you well with your project
ASKER
thanks for help @Julian. Highly appreciated your effort!!
ASKER
problem solved!
Open in new window
To thisOpen in new window
Note the '&' in front of the $value - this tells PHP to treat $value as a reference to the value and not the value itself.