Search code examples
phparrayssearcharray-key-exists

PHP - Searching an array


I have an Array like the following:

Array ( 
[0] => Array ( 
[slideID] => 3 
[parentSlideID] => 1 
[subSlideOrder] => 1 
[headline] => 
[copy] => 
[colourID] => 0 
[URL] => 2.jpg 
[category] => 1 
[visible] => 1 
[slideOrder] => 2 
[type] => 0 ) 

[1] => Array ( 
[slideID] => 3 
[parentSlideID] => 1 
[subSlideOrder] => 1 
[headline] => 
[copy] => 
[colourID] => 0 
[URL] => 2.jpg 
[category] => 1 
[visible] => 1 
[slideOrder] => 2 
[type] => 0 ) 

[2] => Array ( 
[slideID] => 3 
[parentSlideID] => 0 
[subSlideOrder] => 1 
[headline] => 
[copy] => 
[colourID] => 0 
[URL] => 2.jpg 
[category] => 1 
[visible] => 1 
[slideOrder] => 2 
[type] => 0 ) 
) 

How can I search the Array so that I can check the value of parentSlideID in each of the sub Array, and then return the keys where it finds a match.

For example, searching the array for "1" would return 0,1. Searching for "0" would return 2. Searching for "3" wouldn't return anything. Is this possible?


Solution

  • This should help:

    foreach($array as $key=>$value)
    {
        if($value['parentSlideID'] == $searchvalue)
            $results[]=$key;
    }