简体   繁体   中英

How can I search a multidimensional array?

I am trying to find a value in array by searching different value

this is my array

Array
(
    [0] => Array
        (
            [triggerOn] => 07/19/2013 04:32 PM
            [isAppointment] => 0
            [engine_id] => 112
        )

    [1] => Array
        (
            [triggerOn] => 07/26/2013 04:32 PM
            [isAppointment] => 1
            [engine_id] => 111
        )

)

I am trying to find the value of triggerOn When engine id = 111?

So if I have the value 111 then I need to return 07/26/2013 04:32 PM if I have the value 112 then I need to return 07/19/2013 04:32 PM

How can I do this?

This is what I have tried so far

function returnValueOfArray($arr, $val){

    foreach($arr AS $v){


        foreach($v AS $sub){
            if($val == $sub)
            return $v['triggerOn'];
        }

    }
return 'Nothing Found';
}

but this is not working.

function findEngine_id($engine, $array){
    foreach($array as $item){
        if($item['engine_id'] == $engine) return $item['triggerOn'];

    }
    return false;
}
echo findEngine_id(111,$array);
function search_in_array($array, $engine_id){
 foreach($array as $key => $val){
  if($engine_id == $val['engine_id']){
   return $val['triggerOn'];
  }
 }
}

another way to do is :

// let's assume your array is called $myArray


for($i=0; $i < count($myArray) ; $i++ ){
  if($myArray[$i]['engine_id'] == '111'){
    return  $myArray[$i]['triggerOn'];
    break;
  }
}

in a function it gives this :

function find_engine($engine, $myArray){
    for($i=0; $i < count($myArray) ; $i++ ){
      if($myArray[$i]['engine_id'] == $engine){
        return  $myArray[$i]['triggerOn'];
        break;
      }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM