要在PHP 2-维数组中检索数组的特定索引?

时间:2011-03-01 12:20:27

标签: php

我有一个这样的数组

在下面提到的数组中,我必须找到具有meta_key作为data2

的数组的索引
Array ( [0] => Array  ( [meta_id] => 43
                        [post_id] => 12
                        [meta_key] => job
                        [meta_value] => 11
                      )
        [1] => Array  ( [meta_id] => 44
                        [post_id] => 12
                        [meta_key] => data2
                        [meta_value] => Vinodh
                      )
        [2] => Array  ( [meta_id] => 45 
                        [post_id] => 12
                        [meta_key] => data3
                        [meta_value] => Kumar
                      )
        [3] => Array  ( [meta_id] => 46 
                        [post_id] => 12 
                        [meta_key] => data4 
                        [meta_value] => vinodh@rnftechnologies.com 
                      )
        [4] => Array  ( [meta_id] => 47 
                        [post_id] => 12 
                        [meta_key] => data6 
                        [meta_value] => Murugeshpalya 
                      )
        [5] => Array  ( [meta_id] => 48 
                        [post_id] => 12 
                        [meta_key] => data7 
                        [meta_value] => Bangalore 
                      ) 
        [6] => Array  ( [meta_id] => 49 
                        [post_id] => 12 
                        [meta_key] => data8 
                        [meta_value] => 560031 
                      ) 
        [7] => Array  ( [meta_id] => 50 
                        [post_id] => 12 
                        [meta_key] => data9 
                        [meta_value] => India 
                      ) 
        [8] => Array  ( [meta_id] => 51 
                        [post_id] => 12 
                        [meta_key] => data11 
                        [meta_value] => 9638527410 
                      ) 
        [9] => Array  ( [meta_id] => 52 
                        [post_id] => 12 
                        [meta_key] => data13 
                        [meta_value] => Yes 
                      ) 
        [10] => Array ( [meta_id] => 53 
                        [post_id] => 12 
                        [meta_key] => data14 
                        [meta_value] => VTU 
                      ) 
        [11] => Array ( [meta_id] => 54 
                        [post_id] => 12 
                        [meta_key] => data15 
                        [meta_value] => B.E. 
                      ) 
        [12] => Array ( [meta_id] => 55 
                        [post_id] => 12 
                        [meta_key] => data18 
                        [meta_value] => I have read and understood the privacy policy. 
                      ) 
        [13] => Array ( [meta_id] => 76 
                       [post_id] => 12 
                       [meta_key] => interview 
                       [meta_value] => 15 
                      ) 
        [14] => Array ( [meta_id] => 77 
                        [post_id] => 12 
                        [meta_key] => rating 
                        [meta_value] => 0 
                      ) 
        [15] => Array ( [meta_id] => 79 
                        [post_id] => 12 
                        [meta_key] => interview 
                        [meta_value] => 16 
                      ) 
        [16] => Array ( [meta_id] => 82 
                        [post_id] => 12 
                        [meta_key] => contactmail 
                        [meta_value] => 17 
                      )
      ) 

提前致谢

3 个答案:

答案 0 :(得分:1)

function get_index($array, $meta_key)
{
  for($i=0;$i<sizeof($array);$++)
  {
    if($array[$i]['meta_key'] == $meta_key) return $i;
  }
}

用法:

$array = array( ... );

$index = get_index($array, 'data2');

答案 1 :(得分:0)

你可以尝试:

foreach($key1 as array_keys($array) {
    $key2 = array_search('meta_key', $array[$key1]);
}

检索'meta_key'的$ key

答案 2 :(得分:0)

这应该适合你:

$output=array();
    foreach ($arr_data as $key=>$value)
    {
       if($value['meta_key'] == 'data2')
       {
         $output[$key] = $value;
       }
    }

    echo "<pre>";
    print_r($output);