dpyic24480 2014-06-14 10:55
浏览 100
已采纳

在多维数组中查找子数组

I have a multidimensional array and would like to find an array by specific payment_status: My array below and I would like to return an array by payment_status.

[orders] => Array
        (
            [0] => Array
                (
                    [total_price] => 995.94
                    [avg] => 995.940000
                    [count] => 1
                    [payment_status] => 1
                )

            [1] => Array
                (
                    [total_price] => 779.64
                    [avg] => 779.640000
                    [count] => 1
                    [payment_status] => 2
                )

            [2] => Array
                (
                    [total_price] => 1763.49
                    [avg] => 1763.490000
                    [count] => 1
                    [payment_status] => 3
                )

function get_sub_array((int)$payment_status) {

}

How can I do this? Thanks!

  • 写回答

4条回答 默认 最新

  • dtv8189 2014-06-14 11:02
    关注
    $arr = array('orders' => array
            (
                0 => Array
                    (
                        'total_price' => 995.94,
                        'avg' => 995.940000,
                        'count' => 1,
                        'payment_status' => 1
                    ),
    
                1 => Array
                    (
                        'total_price' => 779.64,
                        'avg' => 779.640000,
                        'count' => 1,
                        'payment_status' => 2
                    ),
    
                2 => Array
                    (
                        'total_price' => 1763.49,
                        'avg' => 1763.490000,
                        'count' => 1,
                        'payment_status' => 3
                    )
            ));
    
    function get_sub_array($payment_status,$arr) {
      $arrRet = array();
      foreach($arr as $key=>$val){
        if($val['payment_status'] == $payment_status)
          array_push($arrRet,$val);
      }
     return $arrRet;
    }
    
    $arrRes = get_sub_array(1,$arr['orders']);
    print_r($arrRes);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?