search multidimensional array by key and return array value's as result

The array (it should not be important what it looks like)

Array
(
    [0] => Array
        (
            [raam] => Array
                (
                    [item_id] => 1435
                    [item_name] => 3 voudig afdekraam
                    [friendly_url] => busch-jaeger-schakelmateriaal/reflex-si/afdekraam/3-voudig/370236
                    [small_image] => images/small/1725-0-0944.jpg
                    [manufacturer_code] => 370236
                    [price] => 6.14
                    [sales_price] => 3.99
                    [type] => 214
                    [voudig] => 3
                )

            [items] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [item_id] => 1412
                                    [item_name] => 3 standenschakelaar voor ventilator
                                    [friendly_url] => busch-jaeger-schakelmateriaal/reflex-si/overige-inbouwsokkels-met-centraalplaat/3 standenschakelaar-voor-ventilator/370507
                                    [small_image] => images/small/1164-0-0144.jpg
                                    [manufacturer_code] => 370507
                                    [price] => 25.65
                                    [sales_price] => 16.67
                                    [type] => cen_207
                                    [img] => images/bj/centraal/214/ventilatie.png
                                )

                        )

                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [item_id] => 1412
                                    [item_name] => 3 standenschakelaar voor ventilator
                                    [friendly_url] => busch-jaeger-schakelmateriaal/reflex-si/overige-inbouwsokkels-met-centraalplaat/3 standenschakelaar-voor-ventilator/370507
                                    [small_image] => images/small/1164-0-0144.jpg
                                    [manufacturer_code] => 370507
                                    [price] => 25.65
                                    [sales_price] => 16.67
                                    [type] => cen_207
                                    [img] => images/bj/centraal/214/ventilatie.png
                                )

                        )

                    [2] => Array
                        (
                            [0] => Array
                                (
                                    [item_id] => 1391
                                    [item_name] => Trekschakelaar
                                    [friendly_url] => busch-jaeger-schakelmateriaal/busch-duro-2000-si/overige-inbouwsokkels-met-centraalplaat/trekschakelaar/370085
                                    [small_image] => images/small/1315-0-0423.jpg
                                    [manufacturer_code] => 370085
                                    [price] => 19.07
                                    [sales_price] => 12.40
                                    [type] => cen_206
                                    [img] => images/bj/centraal/214/trek.png
                                )

                            [1] => Array
                                (
                                    [item_id] => 1411
                                    [item_name] => Centraalplaat trekschakelaar
                                    [friendly_url] => busch-jaeger-schakelmateriaal/reflex-si/overige-inbouwsokkels-met-centraalplaat/centraalplaat-trekschakelaar/370318
                                    [small_image] => images/small/1753-0-5788.jpg
                                    [manufacturer_code] => 370318
                                    [price] => 3.55
                                    [sales_price] => 2.31
                                    [type] => cen_206
                                    [img] => images/bj/centraal/214/trek.png
                                )

                        )

                )

            [name] => naamsloos
            [prijs] => 80.06
            [sales] => 52.04
        )

    [1] => Array
        (
            [raam] => Array
                (
                    [item_id] => 1433
                    [item_name] => 1 voudig afdekraam
                    [friendly_url] => busch-jaeger-schakelmateriaal/reflex-si/afdekraam/1-voudig/370514
                    [small_image] => images/small/1725-0-0928.jpg
                    [manufacturer_code] => 370514
                    [price] => 2.07
                    [sales_price] => 1.35
                    [type] => 214
                    [voudig] => 1
                )

            [items] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [item_id] => 1412
                                    [item_name] => 3 standenschakelaar voor ventilator
                                    [friendly_url] => busch-jaeger-schakelmateriaal/reflex-si/overige-inbouwsokkels-met-centraalplaat/3 standenschakelaar-voor-ventilator/370507
                                    [small_image] => images/small/1164-0-0144.jpg
                                    [manufacturer_code] => 370507
                                    [price] => 25.65
                                    [sales_price] => 16.67
                                    [type] => cen_207
                                    [img] => images/bj/centraal/214/ventilatie.png
                                )

                        )

                )

            [name] => naamsloos
            [prijs] => 27.72
            [sales] => 18.02
        )

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

Open in new window



the function:

function search_by_key($key, $array){
  the function.....
}

Open in new window


call the result:


print_r(search_by_key( "item_id", $array));

Open in new window



result:

array of all values searched by key:
Array
(
    [0] => 1435
    [1] => 1412
    [2] => 1412
    [3] => 1391
    [4] => 1411
)

Open in new window

this is just a snippet of the total result's found by key "item_id"


Does someone already have a function?

function search_by_key($key, $array){
  the function.....
}
LVL 1
ScottNL1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
Hello,

There is built in function callde "array_key_exists" you can use it:
bool array_key_exists ( mixed $key , array $search )

Open in new window


For more details see this link:
http://php.net/manual/en/function.array-key-exists.php

Open in new window

ScottNL1Author Commented:
i am aware of this function, but it does not search in multidimensional array's and it does not return the array values.


array_key_exists("item_id",$array);

will return false. I want the values (in a array) that have the key item_id in any dimension of the array.
Ahmed MerghaniSoftware EngineerCommented:
Sorry for that. This is the function:

function search($array, $key, $value)
{
    $results = array();

    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array;

        foreach ($array as $subarray)
            $results = array_merge($results, search($subarray, $key, $value));
    }

    return $results;
}

Open in new window


It will return the result in array. See this link:

http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php

Open in new window

ScottNL1Author Commented:
almost right. I don't have the value, only the the key.

Here's the modified code to return all values (in a array) within multidimensional array by key.

function search($array, $key)
{
    $results = array();

    if (is_array($array))
    {
        if (isset($array[$key]))
            $results[] = $array[$key];

        foreach ($array as $subarray)
            $results = array_merge($results, search($subarray, $key));
    }

    return $results;
}

print_r(search($array,"item_id"));exit;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ScottNL1Author Commented:
Other solution was almost right.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.