1

I have been scratching my head over this code from last 30 min.

$orderData = $orderData->get();
//var_dump($orderData);exit;
$orderFinal = array();
foreach ($orderData as $order) {
    //var_dump($order->id);
    if(in_array($order->id, $orderFinal)){
        $orderFinal[$order->id] = (array) $order;
    }else{
        $orderFinal[$order->id] = (array) $order;
    }
    // var_dump($orderFinal[$order->id]);
}
var_dump($orderFinal);exit;

$OrderData looks like this->

array(4) {
  [0]=>
  object(stdClass)#299 (7) {
    ["id"]=>
    int(1)
    ["created_at"]=>
    string(19) "2016-09-16 12:07:18"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(11)
    ["order_title"]=>
    string(10) "Some title"
  }
  [1]=>
  object(stdClass)#300 (7) {
    ["id"]=>
    int(32)
    ["created_at"]=>
    string(19) "2016-09-16 10:03:50"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(16) "new machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(12)
    ["order_title"]=>
    string(9) "asdasdasd"
  }
  [2]=>
  object(stdClass)#301 (7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(28) "another awesome machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(123)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
  [3]=>
  object(stdClass)#302 (7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(1022)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
}  

I want result like this:

array(3) {
  [1]=>
  array(7) {
    ["id"]=>
    int(1)
    ["created_at"]=>
    string(19) "2016-09-16 12:07:18"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(11)
    ["order_title"]=>
    string(10) "Some title"
  }
  [32]=>
  array(7) {
    ["id"]=>
    int(32)
    ["created_at"]=>
    string(19) "2016-09-16 10:03:50"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(16) "new machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(12)
    ["order_title"]=>
    string(9) "asdasdasd"
  }
  [35]=>
        [0]=>array(7) {
        ["id"]=>
        int(35)
        ["created_at"]=>
        string(19) "2016-09-16 10:07:17"
        ["status"]=>
        int(0)
        ["part_name"]=>
        string(28) "another awesome machine part"
        ["ordered_by"]=>
        int(1)
        ["quantity"]=>
        int(123)
        ["order_title"]=>
        string(15) "Some Order Name"
        },
  [1]=>array(7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(1022)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
}

What I am getting is -

array(3) {
  [1]=>
  array(7) {
    ["id"]=>
    int(1)
    ["created_at"]=>
    string(19) "2016-09-16 12:07:18"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(11)
    ["order_title"]=>
    string(10) "Some title"
  }
  [32]=>
  array(7) {
    ["id"]=>
    int(32)
    ["created_at"]=>
    string(19) "2016-09-16 10:03:50"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(16) "new machine part"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(12)
    ["order_title"]=>
    string(9) "asdasdasd"
  }
  [35]=>
  array(7) {
    ["id"]=>
    int(35)
    ["created_at"]=>
    string(19) "2016-09-16 10:07:17"
    ["status"]=>
    int(0)
    ["part_name"]=>
    string(14) "some part name"
    ["ordered_by"]=>
    int(1)
    ["quantity"]=>
    int(1022)
    ["order_title"]=>
    string(15) "Some Order Name"
  }
}

Any help will be highly appreciated. Thanks

2 Answers 2

1

Try below code:

 $orderData = $orderData->get();
 $orderFinal = array();
 foreach ($orderData as $order) {
    //var_dump($order->id);
     if(array_key_exists($order->id, $orderFinal)){

        if (! isset($orderFinal[$order->id][0] ) ){
            $orderFinal[$order->id][0] = $orderFinal[$order->id];
        }
        $orderFinal[$order->id][] = (array) $order;

    }else{
        $orderFinal[$order->id] = (array) $order;
    }
    // var_dump($orderFinal[$order->id]);
}
var_dump($orderFinal);exit;
Sign up to request clarification or add additional context in comments.

Comments

0

Both branches of your if statement do the same thing, so obviously something's not right. This should do the trick:

$orderFinal = array();
foreach ($orderData as $order) {
    if (in_array($order->id, array_keys($orderFinal))) {
        if (isset($orderFinal[$order->id]["id"])) {
            $orderFinal[$order->id] = array($orderFinal[$order->id], (array) $order);
        } else {
            $orderFinal[$order->id][] = (array) $order;
        }
    } else {
        $orderFinal[$order->id] = (array) $order;
    }
}

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.