Ali Demirci
Ali Demirci

Reputation: 5442

how to get key of a value that in array of array?

I can't deal with this. Please help me. This is array:

$arr = array("data" => array(
                    array("id" => "5451"),
                    array("id" => "45346346")
                    ));

for example how can i find the key for id 45346346 ?

$key = array_search($arr['data'], 45346346);

I have tried this but its not working.

I'm trying to delete that array line. I'm guessing I can do that with unset($key)

Upvotes: 2

Views: 71

Answers (1)

gen_Eric
gen_Eric

Reputation: 227260

You have an array of array of arrays. $arr['data'] is an array with 2 values. These values are both arrays. array_search doesn't work, as 45346346 doesn't match an array.

You'd have you cook your own search, something like this:

function find_in_array($arr, $val){
   $found = false;
   foreach($arr as $k=>$x){
      if(array_search($val, $x) !== FALSE){
         $found = $k;
         break;
      }
   }
   return $found;
}

Then you can do: $key = find_in_array($arr['data'], 45346346);. This will return 1, the index of the array containing 'id' => 45346346 inside $arr['data'].

DEMO: http://codepad.org/pSxaBT9g

Upvotes: 1

Related Questions