2015-10-15 55 views
1

一個特定的值,我有這個數組的一個multiimentional陣列的所有鍵:拿到賽在PHP

$transfers = 
    Array 
    (
     [0] => Array 
      (
       [agency_to_id] => 8 
       [agency_name] => 3.1 SUCURSAL BRASILIA 
       [product_id] => 415 
       [product_code] => 111021 
       [product_name] => PAN FELIPE POR KILO 
       [ptype_id] => 54 
       [ptype_name] => 1.1.01.1 PANADERIA X KILO 
       [catalog_id] => 1 
       [subject_id] => 3 
       [label_id] => 300000002 
       [total_quantity] => 12 
      ) 

     [1] => Array 
      (
       [agency_to_id] => 9 
       [agency_name] => 4.1 SUCURSAL CENTRO 
       [product_id] => 415 
       [product_code] => 111021 
       [product_name] => PAN FELIPE POR KILO 
       [ptype_id] => 54 
       [ptype_name] => 1.1.01.1 PANADERIA X KILO 
       [catalog_id] => 1 
       [subject_id] => 3 
       [label_id] => 300000002 
       [total_quantity] => 8 
      ) 

     [2] => Array 
      (
       [agency_to_id] => 8 
       [agency_name] => 3.1 SUCURSAL BRASILIA 
       [product_id] => 416 
       [product_code] => 111024 
       [product_name] => GALLETA POR KILO 
       [ptype_id] => 54 
       [ptype_name] => 1.1.01.1 PANADERIA X KILO 
       [catalog_id] => 1 
       [subject_id] => 3 
       [label_id] => 300000002 
       [total_quantity] => 1.6 
      ) 

     [3] => Array 
      (
       [agency_to_id] => 8 
       [agency_name] => 3.1 SUCURSAL BRASILIA 
       [product_id] => 418 
       [product_code] => 111028 
       [product_name] => PAN INTEGRAL POR KILO 
       [ptype_id] => 54 
       [ptype_name] => 1.1.01.1 PANADERIA X KILO 
       [catalog_id] => 1 
       [subject_id] => 3 
       [label_id] => 300000002 
       [total_quantity] => 200 
      ) 
    ) 

我想匹配特定的子陣列值,例如該陣列的所有鍵我想得到與[product_id] => 415匹配的密鑰,我應該得到密鑰0和1

我試過用array_keys,但它不起作用。

編輯:

foreach ($transfers $key => $transfer) { 
        $found_keys = array_keys($transfers, $transfer['product_id']); 
       } 

所以,你的回答,轉予空數組

foreach ($transfers $key => $transfer) { 
         $filteredKeys = array_keys(array_filter($transfers, 
             function($item) { 
              return $item['product_id'] === $transfer['product_id']; 
             })); 
        } 

你能不能幫我請。謝謝

+0

array_keys不搜索多維數組。你必須在數組上循環並自己搜索每個子數組。 –

+0

我會從@sergio發佈'array_column()'方法。 – AbraCadaver

回答

5

您的編輯後:

$found_keys = array(); 
foreach ($transfers as $key => $transfer) { 
    if ($transfer['product_id'] === 415) $found_keys[] = $key; 
} 

的最初規定問題的解決方案如下圖:

使用array_filter像這樣:

$filtered = array_filter($transfers, 
         function($item) { 
          return $item['product_id'] === 415; 
         }); 

把所有匹配的元素,完整。

要只拿到鑰匙,將結果傳遞給array_keys

$filteredKeys = array_keys(array_filter($transfers, 
             function($item) { 
              return $item['product_id'] === 415; 
             })); 

這工作,因爲array_filter保留所得數組中源陣列的鑰匙。

+0

非常高效,+1!如果需要,OP仍然可以自由地調整回調來製作更復雜的過濾器。 –

+0

我不能讓它工作。因爲我在每個循環內傳遞數組 – juanpscotto

+0

然後請發佈您的代碼並陳述您的真實問題 - 解決錯誤的問題既浪費我們的時間。 –

3

聽起來像是foreach工作:

$found_keys = array(); 
foreach($transfers as $transfer){ 
    if($transfer['product_id'] == 415){ 
     array_push($found_keys, $transfer); 
    } 
} 
1

使用array_columnarray_keys與搜索參數。下面是它如何工作的示例:

$products = array(
    0 => array('product_id'=>3), 
    1 => array('product_id'=>4), 
    2=> array('product_id'=>3) 
);    

$keys = array_keys(array_column($products, 'product_id'), 3); 
// outputs the key of the element that has `product_id` = 3 
var_dump($keys); 
0

循環,循環和循環。這是關鍵。 (獲取它主要對不起?)

總之:

$array = array(
    'bla1' = array(
     'subbla1' => 'subval1', 
     'subbla2' => 'subval2', 
     'subbla3' => 'subval3', 
     'subbla4' => 'subval4' 
    ), 
    'bla2' = array(
     'subbla1' => 'subval1', 
     'subbla2' => 'subval2', 
     'subbla3' => 'subval3', 
     'subbla4' => 'subval4' 
    ), 
    'bla3' = array(
     'subbla1' => 'subval1', 
     'subbla2' => 'subval2', 
     'subbla3' => 'subval3', 
     'subbla4' => 'subval4' 
    ), 
); 

foreach ($array as $key => $value) { 
    foreach ($value as $subkey => $subvalue) { 
     // here you have $key, $value, $subkey and $subvalue at your disposal. 
    } 
}