After receiving data from my db I put this data in an array row by row so I can access this array with al the required data later on. But I'm currently using following array:
JSON SAMPLE
[{"orderID":5,"customerID":2,"customerName":"Jelle Hofman","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":4,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":false,"extrasPrice":0},
{"orderID":5,"customerID":2,"customerName":"Jelle Hofman","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":5,"productTitle":"Sexy Teacher","productPrijs":4,"aantal":3,"extras":false,"extrasPrice":0}]
As you can see I got 2 times orderID (5) with each time the orderdetails which all belong to the same orderID, in this example orderID 5.
Now this array isn't great because when using angular I have difficulties showing the orders per orderID and the orderdetails which belong to that orderID.
What I want to show is for each orderID:
is something like: foreach orderdetailID in orderID.
orderID 5 => all of its orderDetails (= data of orderdetailID 4 AND orderdetailID 5)
So when creating that array, how can I add an array of orderdetails to the row of the correspondent orderID?
example:
[{"orderID":5,"customerID":2,"customerName":"Jelle Hofman","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare", {"orderdetailID":4,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":false,"extrasPrice":0},{"orderdetailID":5,"productTitle":"Sexy Teacher","productPrijs":4,"aantal":3,"extras":false,"extrasPrice":0}]
You need to loop over it and create a nested structure. I'd probably do this on the PHP side but you could do it on the JS side if you wanted
$orders = array();
// Let's create an array of the keys that belong to the "order"
// we will use this to extract the other keys for the details
$orderKeys = array('orderID', 'customerID', 'customerName', 'orderDate', 'orderDeliveryDate', 'orderStatus');
$orderKeys = array_combine($orderKeys, $orderKeys);
// loop over each row in the db result set - not sure
// what driver you are using so you can replace this loop
// with whatever kind of loop makes sense
foreach($resultSet as $row) {
$id = $row['orderID'];
if (!isset($orders[$id]) {
// This is the first record in our result set for this
// orderID, so let's get the data for the order and put it
// into the orders array, using the orderID as the array key
$orders[$id] = array_intersect_key($row,$orderKeys);
// Let's also add an array for all our details items
// you could use a key other than details just depends on
// what you want to call it
$orders[$id]['details'] = array();
}
// add the detail item to the order
$orders[$id]['details'][] = array_diff_key($row, $orderKeys);
}
// create the JSON - use array_values to re-index the array
// since using the orderID as the key is not relevant on the JS side
$ordersJSON = json_encode(array_values($orders));
// now do whatever you need to do with the JSON