0

I have an Multidimensional array that takes a similar form to this array bellow.

   $shop = array( array( Title => "rose", 
                  Price => 1.25,
                  Number => 15 
                ),
           array( Title => "daisy", 
                  Price => 0.75,
                  Number => 25,
                ),
           array( Title => "orchid", 
                  Price => 1.15,
                  Number => 7 
                )
         );

I would like to see if a value I'm looking for is in the array, and if so, return the position of the element in the array.

4 Answers 4

5

Here's a function off the PHP Manual and in the comment section.. Works like a charm.

<?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
       $current_key=$key;
       if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

Found this function in the PHP docs: http://www.php.net/array_search

Sign up to request clarification or add additional context in comments.

Comments

2

You can use array_map with in_array and return the keys you want

$search = 1.25; 

print_r(

array_filter(array_map(function($a){

    if (in_array($search, $a)){
        return $a;
    }

}, $shop))

);

Will print:

Array
(
    [0] => Array
        (
            [Title] => rose
            [Price] => 1.25
            [Number] => 15
        )

)

Comments

2

A more naive approach than the one showed by Zander, you can hold a reference to the outer key and inner key in a foreach loop and store them.

$outer = "";
$inner = "";
foreach($shop as $outer_key => $inner_array){
  foreach($inner_array as $inner_key => $value) {
    if($value == "rose") {
      $outer = $outer_key;
      $inner = $inner_key;
      break 2;
    }
  }
}

if(!empty($outer)) echo $shop[$outer][$inner];
else echo "value not found";

3 Comments

Thanks, but what does the break 2; do in this case. My guess is break out of both loops?
@mornenel it breaks out of the two foreach, if there was one foreach you could have simply written break, since there are two you have to specify the level you want to break out of.
May be you forgot to add the open { in the if statement ?
-1

php >= 5.5

$shop = array( array( 'Title' => "rose", 
                  'Price' => 1.25,
                  'Number' => 15 
                ),
           array( 'Title' => "daisy", 
                  'Price' => 0.75,
                  'Number' => 25,
                ),
           array( 'Title' => "orchid", 
                 'Price' => 1.15,
                  'Number' => 7 
                )
         );

         $titles = array_column($shop,'Title');

        if(!empty($titles['rose']) && $titles['rose'] == 'YOUR_SEARCH_VALUE'){
            //do the stuff
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.