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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向