Re: multi dimension array sort help

From: Date: Thu, 05 Feb 2004 14:03:29 +0000
Subject: Re: multi dimension array sort help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-176890@lists.php.net to get a copy of this message
Assuming your array is called $array1: $paths = array(); foreach ($array1 as $data_ptr => $data) { if (!in_array($data['path'],$paths) $paths[] = $data; } sort($paths) foreach($paths as $path_to_check) { foreach($array1 as $data_ptr => $data) { if ($data['path'] == $path_to_check) { /** Do your stuff here **/ } } } It's a little cumbersome and probably has way too many loops, but processor time is cheap and memory is even cheaper. I've found PHP's array parsing to be fairly quick. Alternatively, use the path as the index for your array; i.e: $array1 = array("path" => array("path_id" => 3, "year" => 2004, "month" => "02", "day" => "05", "hits" => 3); When you're parsing your log (which is what I'm assuming you're doing), you would populate $array1 as follows: $array1 = array(); while (parsing()) { $array1[$path]['path_id'] = $path_id; $array1[$path]['year'] = $year; /** etc... **/ } $paths = array_keys($array1); sort($paths); foreach($paths as $path_to_check) { /** Do stuff with $array1[$path_to_check] **/ } Matt Array > ( > [0] => Array > ( > [path_id] => 3 > [year] => 2004 > [month] => 02 > [day] => 05 > [hits] => 3 > [path] => home > ) > > [1] => Array > ( > [path_id] => 2 > [year] => 2004 > [month] => 02 > [day] => 05 > [hits] => 1 > [path] => plan > ) > > [2] => Array > ( > [path_id] => 7 > [year] => 2004 > [month] => 02 > [day] => 05 > [hits] => 1 > [path] => specials > ) > > ) > On Wed, 2004-02-04 at 23:33, Justin French wrote: > Hi, > > I've read and re-read array_multisort(), but still can't get a grip on > how to sort my array: > > Array > ( > [0] => Array > ( > [path_id] => 3 > [year] => 2004 > [month] => 02 > [day] => 05 > [hits] => 3 > [path] => home > ) > > [1] => Array > ( > [path_id] => 2 > [year] => 2004 > [month] => 02 > [day] => 05 > [hits] => 1 > [path] => plan > ) > > [2] => Array > ( > [path_id] => 7 > [year] => 2004 > [month] => 02 > [day] => 05 > [hits] => 1 > [path] => specials > ) > > ) > > > In this case, I'd like to sort the entire array on [path]. Can anyone > provide some tips or an example to start from? > > Justin French

Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc
« previous php.general (#176890) next »