获取与PHP中的特定值匹配的多维数组的所有键

获取与PHP中的特定值匹配的多维数组的所有键

问题描述:

I have this array:

$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
            )
    )

And I want to get all the keys of this array that match a specific subarray value, for example I want to get the keys that match [product_id] => 415 and I should get the the keys 0 and 1

I have tried with array_keys but it does not work.

EDIT:

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

So, your answer, devolves an empty array

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

Can you help me please. Thank you

After your edit:

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

Below the solution for the originally stated problem:

Use array_filter like so:

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

to get all matching elements, complete.

To only get the keys, pass the result to array_keys:

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

This works, because array_filter preserves the keys of the source-array in the resulting array.

Sounds like a job for foreach:

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

Use array_column and array_keys with a search parameter. Here is the example of how it works:

$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);

Loops, loops and loops. That's the key. (Get it? Key? I'm sorry)

Anyhow:

$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.
    }
}