• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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.....
}
0
ScottNL1
Asked:
ScottNL1
  • 3
  • 2
2 Solutions
 
Ahmed MerghaniCommented:
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

0
 
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.
0
 
Ahmed MerghaniCommented:
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

0
 
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

0
 
ScottNL1Author Commented:
Other solution was almost right.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now