Ali_k
Ali_k

Reputation: 1661

Search multidimensional array by value for parent sibling value

I have a multidimensional array that I need to search for some specific values, here is the array:

$Array = Array
    (
        Array
            (
                'sibling' => 'Product 1',
                'another_sibling' => 'not important',
                'parent' => Array
                    (
                        '0' => Array
                            (
                                'product-name' => 'Placeholder name 0',
                            ),

                        '1' => Array
                            (
                                'product-name' =>  'Placeholder name 1',
                            ),

                        '2' => Array
                            (
                                'product-name' => 'Placeholder name 2',
                            )

                    )

            ),

        Array
            (
                'sibling' => 'Product 2',
                'another_sibling' => 'not important',
                'parent' => Array
                    (
                        '0' => Array
                            (
                                'product-name' => 'Placeholder name 3',
                            )

                    )

            )

    );

Now I want to get the 'sibling' value based on 'product-name' value, say I have the following:

$RequestData = Array
(
    '0' => 'Placeholder name 0',
    '1' => 'Placeholder name 1',
    '2' => 'Placeholder name 3',
);

I need a function to take $RequestData and search inside the $array for 'sibiling' value, then return something like this:

Product 1

Product 2

I was able to do something like this, but it will return an array for each item, I want to merge items of same product under same array:

function find($RequestData , $array){

   foreach ($RequestData as $item) {

       foreach ($array as $key => $val) {

            foreach ($val['parent'] as $key0 => $val0) {


               if ($val0['product-name'] == $item) {


                   $result[] = array(
                                'product' => $val['sibling'],
                                'items' => array (
                                    $item,
                                ),
                                );
               }
            }
       }
    }

       return $result;
}

Upvotes: 0

Views: 274

Answers (1)

SamuPert
SamuPert

Reputation: 69

Try this function:

function find($RequestData, $array)
{

    $result = [];

    $elementsReq = count($RequestData);

    foreach ($array as $value)
    {

        $curProd = $value['sibling'];

        $elem =  [];

        foreach ($value['parent'] as $parent)
        {
            for ($i=0; $i < $elementsReq; $i++)
            {


                if($parent['product-name'] ==  $RequestData[$i])
                {
                    array_push($elem, $RequestData[$i]);
                    break;
                }
            }

        }


        $curProdArray = array
        (
            'sibling' => $curProd, 
            'products-name' => $elem
        );

        array_push($result, $curProdArray);
    }

    return $result;
}

This will output with print_r(find($RequestData, $rray));

Array
(
    [0] => Array
        (
            [sibling] => Product 1
            [products-name] => Array
                (
                    [0] => Placeholder name 0
                    [1] => Placeholder name 1
                )

        )

    [1] => Array
        (
            [sibling] => Product 2
            [products-name] => Array
                (
                    [0] => Placeholder name 3
                )

        )

)

Upvotes: 1

Related Questions