通过内部阵列键和值获得外部阵列键

时间:2015-12-16 11:12:53

标签: php multidimensional-array key

5 Array 

( 
  [0] => stdClass Object ( [question_id] => 1 [question_id_count] => 3 ) 
  [1] => stdClass Object ( [question_id] => 2 [question_id_count] => 3 ) 
  [2] => stdClass Object ( [question_id] => 3 [question_id_count] => 3 ) 
  [3] => stdClass Object ( [question_id] => 4 [question_id_count] => 5 ) 
  [4] => stdClass Object ( [question_id] => 5 [question_id_count] => 3 ) 
)

这里我需要输出3,键 - question_id_count具有值3

1 个答案:

答案 0 :(得分:0)

您可以迭代数组,并在当前对象属性匹配时保存密钥:

$outerKey = null;
foreach($array as $key => $value){
    if($value->question_id == 3){
        $outerKey = $key;
        break;
    }
}
var_dump($outerKey); //int(2)