0

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!

2
  • Thats great, so what have you tried? Commented Jun 14, 2014 at 10:58
  • Function Interface is ready but the contents are not there? Try something. Commented Jun 14, 2014 at 10:59

4 Answers 4

1
$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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Right code below: public static function get_order_by_payment_status($arr, $payment_status) { $result = array(); foreach ($arr as $order) { if ($order['payment_status'] == $payment_status) { $result = $order; } } return $result; }
1

This would work I think.

function get_sub_array($arr, $payment_status) {
    foreach($arr as $item) {
        if($item['payment_status'] === $payment_status) {
            return $item;
        }
    }
}

1 Comment

What if more than 1 item.?
0

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;
    }

Comments

0

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; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.