Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

PHP
    // 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:
PHP
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

PHP
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['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.

PHP
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!
Posted
Updated 11-Mar-18 22:28pm

You need to pass the array by reference. This is done in the definition of findandReplace.
You need
function findandReplace(&$array) { 
 ... 
See PHP: Passing by Reference - Manual[^]
 
Share this answer
 
Comments
Member 13720669 12-Mar-18 4:16am    
Hi, thanks for comment but if you see function A, i am already passing array variable as reference! see this

$childArr = &$tree[$i]['child'];
$childArr = findandReplace($childArr);

although i did put additional & sign in findandReplace function but it didn't effect anything. :(
Peter_in_2780 12-Mar-18 5:52am    
No you are not passing it by reference until you specify it in the definition of findandReplace. Read the manual I referenced.
YAY! finally after 1 day fight with references finally it working. I found the solution to this trickish problem. Although i was passing array in my findandReplace function as reference but i was doing recursion as well. It was losing reference(i believe) while finding particular array in recursion. answer is to put 2 additional references one in the foreach to
$value
and otherone to
findandReplace(&$array)


so final function would be look like this

PHP
function findandReplace(&$array) {
        foreach($array as $key => &$value)
        { 
            if(is_array($value))
            { 
                findandReplace($value); 
            }
            else{
                
                if ($key == 'clientCode' && $value == '06456') {
                    // echo $key." = ".$value."<br />\n";

                    $array['individualCommission'] = '10000000000000';
                    break;
                }
            } 
        }

        return $array;
    }


I hope this will help someone. couldn't find any solution on whole internet!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900