Search code examples
phparraysarray-merge

merge a part of array recursive php


I tried googling it but unable to find anything close. Is it possible to merge two array but actually a part of second array?

These array would be very long and on second I want to just use the categories from second array and merge in first!

My array:

$ar1 = array(
    "locale" => "en-US",
    "id" => 1,
    "categories" => array(
        "0" => array("name" => "abc", "username" => "abc1"),
        "1" => array("name" => "cdf", "username" => "bbb3"),
    )
);

$ar2 = array(
    "locale" => "en-US",
    "id" => 1,
    "categories" => array(
        "0" => array("name" => "xyz", "username" => "xyz4"),
        "1" => array("name" => "zyx", "username" => "xtt44"),
    )
);

I tried array_merge, array_merge_recursive but it does not work.

I am getting

Array
(
    [locale] => Array
        (
            [0] => en-US
            [1] => en-US
        )

    [id] => Array
        (
            [0] => 1
            [1] => 1
        )

    [categories] => Array
        (
            [0] => Array
                (
                    [name] => abc
                    [username] => abc1
                )

            [1] => Array
                (
                    [name] => cdf
                    [username] => bbb3
                )

            [2] => Array
                (
                    [name] => xyz
                    [username] => xyz4
                )

            [3] => Array
                (
                    [name] => zyx
                    [username] => xtt44
                )

        )

)

But this is what I want:

Array
(
    [locale] => Array
        (
            [0] => en-US
        )

    [id] => Array
        (
            [0] => 1
        )

    [categories] => Array
        (
            [0] => Array
                (
                    [name] => abc
                    [username] => abc1
                )

            [1] => Array
                (
                    [name] => cdf
                    [username] => bbb3
                )

            [2] => Array
                (
                    [name] => xyz
                    [username] => xyz4
                )

            [3] => Array
                (
                    [name] => zyx
                    [username] => xtt44
                )

        )

)

Solution

  • This may not be a Built-In Function. Yes; it is just a nested Loop with branched out conditionals. Test it... and see if (most importantly) it does what you want... the rest is a matter of choice: Simplicity over complexity.

    Here are the Given Arrays: $ar1 and $ar2

        <?php
    
            $ar1 = array(
                "locale" => "en-US",
                "id" => 1,
                "categories" => array(
                    "0" => array("name" => "abc", "username" => "abc1"),
                    "1" => array("name" => "cdf", "username" => "bbb3"),
                )
            );
    
            $ar2 = array(
                "locale" => "en-US",
                "id" => 1,
                "categories" => array(
                    "0" => array("name" => "xyz", "username" => "xyz4"),
                    "1" => array("name" => "zyx", "username" => "xtt44"),
                )
            );
    

    And here's to the Looping Construct:

        <?php
    
            $arrResult  = array();
    
            foreach($ar1 as $key=>$value){
                if(!array_key_exists($key, $arrResult)){
    
                    if(is_array($value)){
                        $arrResult[$key] = array();
                        for($i=0; $i<count($value); $i++){
                            $arrResult[$key][] = $value[$i];
                        }
                    }else{
                        $arrResult[$key] = $value;
                    }
    
                    foreach ($ar2 as $index => $item) {
                        if(!array_key_exists($index, $arrResult)){
                            if(is_array($item)){
                                if($key == $index) {
                                    $arrResult[$index] = array();
                                    for ($j = 0; $j < count($item); $j++) {
                                        $arrResult[$key][] = $item[$j];
                                    }
                                }
                            }
                        }else{
                            if(is_array($item)){
                                if($key == $index) {
                                    for ($j = 0; $j < count($item); $j++) {
                                        array_push($arrResult[$index], $item[$j]);
                                    }
                                }
                            }else{
                                $arrResult[$index] = $item;
                            }
                        }
                    }
    
                }
            }
    
            var_dump($arrResult);
    
    ?>
    

    Here is the Output of the var_dump():

        array (size=3)
            'locale' => string 'en-US' (length=5)
            'id' => int 1
        'categories' =>
        array (size=4)
            0 =>
            array (size=2)
                'name' => string 'abc' (length=3)
                'username' => string 'abc1' (length=4)
            1 =>
            array (size=2)
                'name' => string 'cdf' (length=3)
                'username' => string 'bbb3' (length=4)
            2 =>
            array (size=2)
                'name' => string 'xyz' (length=3)
                'username' => string 'xyz4' (length=4)
            3 =>
            array (size=2)
                'name' => string 'zyx' (length=3)
                'username' => string 'xtt44' (length=5)
    

    I still believe you may need to add your own logic (if you like it) depending on your use-case and preference.