Solved

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

Posted on 2012-03-29
5
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

728 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