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

在多维数组中查找子数组

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!

图片转代码服务由CSDN问答提供 功能建议

我有一个多维数组,想通过特定的payment_status找到一个数组: 我在下面的数组,我想要 通过payment_status返回一个数组。

  [orders] => 数组
(
 [0] =>数组
(
 [total_price] => 995.94 
 [平均] => 995.940000 
 [计数] => 1 
 [payment_status] =&gt  ; 1 
)
 
 [1] =>数组
(
 [total_price] => 779.64 
 [avg] => 779.640000 
 [count] => 1 
 [payment_status  ] => 2 
)
 
 [2] =>数组
(
 [total_price] => 1763.49 
 [avg] => 1763.490000 
 [count] => 1 \  n [payment_status] => 3 
)
 
功能get_sub_array((int)$ payment_status){
 
} 
   
 
 

我该怎么办? 这个? 谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    点赞 打赏 评论
  • douhuitan5863 2014-06-14 10:59

    This would work I think.

    function get_sub_array($arr, $payment_status) {
        foreach($arr as $item) {
            if($item['payment_status'] === $payment_status) {
                return $item;
            }
        }
    }
    
    点赞 打赏 评论
  • douzi2778 2014-06-14 11:01

    Assuming you want to retrieve multiple orders with the same status, to return them all you could do:

        function get_sub_array($orders, $payment_status) {
    
            $result = array();
            foreach ($orders as $order) {
                if ($order['payment_status'] == $payment_status) {
                    array_push($result, $order);
                }
            }
    
            return $result;
        }
    
    点赞 打赏 评论
  • dongtui2029 2014-06-14 11:17

    You can use array_filter. For example if you want a sub array containing all values with payment status 3:

    $sub_array = array_filter($arr['orders'],function($val){ 
        return $val['payment_status'] === 3; 
    });
    
    点赞 打赏 评论

相关推荐 更多相似问题