2013-06-28 109 views
1
<?php 
$x = array(
     "C_Card_ID" => array(
           "dbName"=>"CardID", 
           "type"=>"disabled", 
           "key"=>"primary" 
         ), 
     "C_Payroll_ID" => array(
           "dbName"=> "PayrollID", 
           "key"=>"unique" 
         ), 
     "C_First_Name" => array("dbName"=>"FirstName") 
?> 

我想要$x的鍵,它的第二個數組中有"key"索引。簡而言之,我需要C_Card_ID和C_Payroll_ID作爲數組中的輸出,所以稍後我會破壞它們。PHP查找二維數組中的數組鍵

Required output Sample : Array("C_Card_ID","C_Payroll_ID") 

請不要使用循環算法。我需要使用一些內置函數。

回答

2

您可以使用array_filter

語法是:

$filtered_array = array_keys(array_filter($x, function($a){ return isset($a['key']); })); 
+1

是的,正確的,我已經做到了,非常感謝,讚揚你的答案... –

2

這應該這樣做:

$result = array_keys(array_filter($x, function($arr){ 
    return array_key_exists('key', $arr); 
})); 
+0

警告:array_key_exists()預計參數2爲陣列,串在xx.php給定上線XX –

+0

代替array_key_exists( '關鍵',$ ARR); .... isset($ arr ['key'])適用於所有字符串和數組。很好的答案,謝謝 –

2

嘗試使用array_slice()一樣,

<?php 
    $x = array(
      "C_Card_ID" => array(
            "dbName"=>"CardID", 
            "type"=>"disabled","key"=>"primary" 
          ), 
      "C_Payroll_ID" => array(
            "dbName"=> "PayrollID", 
            "key"=>"unique" 
          ), 
      "C_First_Name" => array("dbName"=>"FirstName")); 
    print_r(array_slice(array_keys($x),0,2)); 
    //Outputs 
    //Array ([0] => C_Card_ID [1] => C_Payroll_ID) 
?> 

測試在http://writecodeonline.com/php/

+0

正確,但我使用關聯數組,我的索引不固定在0,1或2等,但有幫助 –