Solved

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

Posted on 2012-03-29
5
255 Views
Last Modified: 2012-04-02
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
Comment
Question by:ScottNL1
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 37780629
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
 
LVL 1

Author Comment

by:ScottNL1
ID: 37780649
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
 
LVL 8

Assisted Solution

by:Ahmed Merghani
Ahmed Merghani earned 500 total points
ID: 37780692
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
 
LVL 1

Accepted Solution

by:
ScottNL1 earned 0 total points
ID: 37780709
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
 
LVL 1

Author Closing Comment

by:ScottNL1
ID: 37795254
Other solution was almost right.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question