开发者

To retrieve a particular index of an array in PHP 2- dimension array?

I have an array of this kind

In the below mentioned array i have to find the index of the array having meta_key as 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 
             开发者_Go百科         ) 
        [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 
                      )
      ) 

Thanks in advance


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

usage:

$array = array( ... );

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


you can try :

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

to retrieve the $key of 'meta_key'


This should work for you:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜