2

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)

3
  • 2
    What answer are you expecting for 45346346? Commented Jan 5, 2012 at 21:28
  • What do you want? id? data? Something else? Commented Jan 5, 2012 at 21:30
  • I have to delete that line with a php function. Commented Jan 5, 2012 at 21:30

1 Answer 1

1

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

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

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.