douaonong7807 2015-10-15 14:11
浏览 68
已采纳

获取与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

  • 写回答

4条回答 默认 最新

  • doujiabing1228 2015-10-15 14:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染