Re: order/reorder pull out data from arrays
| From: | Jochem Maas | Date: | Tue, 11 Jul 2006 20:01:01 +0000 |
| Subject: | Re: order/reorder pull out data from arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239323@lists.php.net to get a copy of this message | ||
Dallas Cahker wrote:
> I have an array that i would like to sort and reorder based on a simple
> critera. I already order the data but I would like to break the data now
> into sections by customer id, so one customer has 5 things and another
> customer has one. How can I do that with an array.
>
> $data = orders($id,$status);
> $c = count($data);
> for($i=0; $i<$c; $i++) {
> $orderid = $data[$i]['orderid'];
> $customerid = $data[$i]['customerid'];
> $name = $data[$i]['name'];
> print $orderid.' - '.$customerid.' - '.$name.'<br>';
> }
// something like this?:
$grouped = array();
foreach ($data as $d) {
$cid = $d['customerid'];
if (!isset($grouped[$cid])) {
$grouped[$cid] = array(
'cust' => "{$cid} - {$d['name']}";
'data' => array();
);
}
// I dont know exactly what constitutes an order line
// - your example doesn't make it clear
$grouped[$cid]['data'][] = "make a string of the current orderline here!";
}
foreach ($grouped as $cid => $data) {
echo $data['cust'],"\n";
foreach ($data['data'] as $orderline) echo $orderline,"\n";
}
> What it is currently is all the orders are ordered by the customer number,
> however I would like to group it a little nicer on the output.
>
...
>
> Anyway I can do that?
as many as there ways to skin a cat probably :-)
>