一個特定的值,我有這個數組的一個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'];
}));
}
你能不能幫我請。謝謝
array_keys不搜索多維數組。你必須在數組上循環並自己搜索每個子數組。 –
我會從@sergio發佈'array_column()'方法。 – AbraCadaver