Home > Blockchain >  Compare two multidimensional key values & combine non duplicates
Compare two multidimensional key values & combine non duplicates

Time:12-09

I have two multidimensional arrays. I'm looping through both arrays, checking for certain values & creating a new array.

$full_cats array

Array
(
[0] => Array
    (
        [parent_cats] => Array
            (
                [id] => 384
                [name] => Beers & Ales
                [parent] => 0
            )

    )

[1] => Array
    (
        [parent_cats] => Array
            (
                [id] => 387
                [name] => Wines
                [parent] => 0
            )

    )

)


$sub_cats array

Array
(
[0] => Array
    (
        [sub_cats] => Array
            (
                [id] => 386
                [name] => Ales
                [parent] => 384
            )

    )

[1] => Array
    (
        [sub_cats] => Array
            (
                [id] => 385
                [name] => Beers
                [parent] => 384
            )

    )

[2] => Array
    (
        [sub_cats] => Array
            (
                [id] => 403
                [name] => Red
                [parent] => 387
            )

    )

)

I'm looping through both arrays to check if $sub_cat['sub_cats']['parent'] value is the same as the $full_cat['parent_cats']['id'] value. If this is true, both values are added to the $master_cats array.

$master_cats = array();
foreach ($sub_cats as $sub_cat) {
    foreach ($full_cats as $full_cat) {
        if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){

            $master_cats[] = array(
                "parent_cats" => array(
                    $full_cat['parent_cats'],
                ),
                "sub_cats" => array(
                    $sub_cat['sub_cats'],
                )
            );

        };
    };
};

The $master_cats output -

Array
(
[0] => Array
    (
        [parent_cats] => Array
            (
                [0] => Array
                    (
                        [id] => 384
                        [name] => Beers & Ales
                        [parent] => 0
                    )

            )

        [sub_cats] => Array
            (
                [0] => Array
                    (
                        [id] => 386
                        [name] => Ales
                        [parent] => 384
                    )

            )

    )

[1] => Array
    (
        [parent_cats] => Array
            (
                [0] => Array
                    (
                        [id] => 384
                        [name] => Beers & Ales
                        [parent] => 0
                    )

            )

        [sub_cats] => Array
            (
                [0] => Array
                    (
                        [id] => 385
                        [name] => Beers
                        [parent] => 384
                    )

            )

    )

[2] => Array
    (
        [parent_cats] => Array
            (
                [0] => Array
                    (
                        [id] => 387
                        [name] => Wines
                        [parent] => 0
                    )

            )

        [sub_cats] => Array
            (
                [0] => Array
                    (
                        [id] => 403
                        [name] => Red
                        [parent] => 387
                    )

            )

    )

)

I'm having two issues with the $master_cats array.

1st problem - As you can see, index 0 & 1 have the same [parent_cats] values. I only want to add the [parent_cats] key/values if they dont already exist.

2nd Problem - the $master_cats array index 0 & 1, the sub_cats array, some values are different but both have the same [sub_cats][parent] => 384 so they belong in the same array index, eg 0.

Below is what I'm hoping to achieve with the $master_cats array from the foreach/loop above

    Array
(
    [0] => Array
        (
            [parent_cats] => Array
                (
                    [0] => Array
                        (
                            [id] => 384 <-- Parent ID
                            [name] => Beers &amp; Ales
                            [parent] => 0
                        )

                )

            [sub_cats] => Array
                (
                    [0] => Array
                        (
                            [id] => 386
                            [name] => Ales
                            [parent] => 384 <-- belongs to [parent_cats][id]
                        )
                    [1] => array 
                        (
                            [id] => 385
                            [name] => Beers
                            [parent] => 384 <-- belongs to [parent_cats][id]
                        )
                )

        )
    [1] => Array
        (
            [parent_cats] => Array
                (
                    [0] => Array
                        (
                            [id] => 387 <-- Parent ID
                            [name] => Wines
                            [parent] => 0
                        )

                )

            [sub_cats] => Array
                (
                    [0] => Array
                        (
                            [id] => 403
                            [name] => Red
                            [parent] => 387 <-- belongs to [parent_cats][id]
                        )

                )

        )

)

CodePudding user response:

Here's one way to go about it. Note: your array structures are a bit over-nested... but here's a solution for the given structure

$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
    $tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
    foreach ($sub_cats as $sub) {
        if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
            $tmp['sub_cats'][]=$sub['sub_cats'];
        }
    }
    $all[] = $tmp;
}
print_r($all);

Example: https://3v4l.org/01oLF

Output:

Array
(
    [0] => Array
        (
            [parent_cats] => Array
                (
                    [id] => 384
                    [name] => Beers &amp; Ales
                    [parent] => 0
                )

            [sub_cats] => Array
                (
                    [0] => Array
                        (
                            [id] => 386
                            [name] => Ales
                            [parent] => 384
                        )

                    [1] => Array
                        (
                            [id] => 385
                            [name] => Beers
                            [parent] => 384
                        )

                )

        )

    [1] => Array
        (
            [parent_cats] => Array
                (
                    [id] => 387
                    [name] => Wines
                    [parent] => 0
                )

            [sub_cats] => Array
                (
                    [0] => Array
                        (
                            [id] => 403
                            [name] => Red
                            [parent] => 387
                        )

                )

        )

)

Note: here's an example of simplifying your array structure (which would require changing the code in my answer, but ultimately might make your life a little easier)

  $parent_cats= array( 
    array(
            "id" => "384",
            "name" => "Beers &amp; Ales",
            "parent" => "0"
        ),
    array(
            "id" => "387",
            "name" => "Wines",
            "parent" => "0"
        ));
  • Related