Re: order/reorder pull out data from arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 :-)

> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux