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 谁会做这个啊#ensp#Boson NetSim
  • ¥15 如何编写针对TPS6503320FRGE型号的电源管理芯片的编程代码?
  • ¥15 设计简单目录管理系统,要满足以下内容
  • ¥15 关于九十度混合耦合器信号分析问题
  • ¥15 Cesm如何关闭不用的模块呢
  • ¥15 vue2两层数据导出为excle
  • ¥15 有人能帮我做一下这个项目吗
  • ¥15 网络上传速率比下载速率高
  • ¥15 (标签-游戏|关键词-网页游戏)
  • ¥15 这个咋搞🥹可能需要手把手教