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.
What I have tried:
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);
}
I was successfully able to find Specific array using
function findandReplace($array) {
foreach($array as $key=>$value)
{
if(is_array($value))
{
findandReplace($value);
}
else{
if ($key == 'clientCode' && $value == '06459') {
$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['individualCommission'] = '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.
function findandReplace($array) {
$parent = &$array;
$iterator = new \RecursiveIteratorIterator( new \RecursiveArrayIterator($array), \RecursiveIteratorIterator::SELF_FIRST );
foreach ($iterator as $key => $item) {
if ( $key === 'clientCode' && $item == '06459') {
$parent = (array)$iterator->getInnerIterator();
$parent['individualCommission'] = '1000000000000000';
break;
}
}
return $array;
}
so i will appreciate if anyone would spare some time and find any solution for this simple problem.
Thanks!