Hi,
I have a handle on this and hope this might help some of you too in
Sorting Multidimensional Arrays. First we need an array.
$user_ar = array(
0 => array( 'id'=>1, 'name'=>joe)
1 => array( 'id'=>2, 'name'=>mike)
2 => array( 'id'=>3, 'name'=>paul)
);
A typical reference is $user_ar[1]['id'] or $user_ar[1]['name']. To
sort by name we need to use the usort() unfunction of PHP:
usort($user_ar, "compare");
where compare is a function:
function compare($x, $y) {
if ( $x['name'] == $y['name'] ){
return 0;
} else if ( $x['name'] < $y['name'] ) {
return -1;
} else {
return 1;
}
}
To sort the same array by different named positions like 'id' replace
'name' with 'id' in the function or create a new function for sorting
by that value. Also to sort DESC change the '<' to '>' in the
function. After calling usort() you can call a foreach() to iterate
the content of $user_ar.
I think that should do it.
Sincerely,
Mike
--
Mike Brandonisio * Web Hosting
Tech One Illustration * Internet Marketing
tel (630) 759-9283 * e-Commerce
[EMAIL PROTECTED] * http://www.jikometrix.net
JIKOmetrix - Reliable web hosting
On Jun 20, 2006, at 7:38 AM, Mike Brandonisio wrote:
> Hi,
>
> I'm having trouble working with Multidimensional Arrays. Does anyone
> have any tutorials or links they might share on the topic. The
> challenges I'm having are sorting by a value in the 2nd array and
> searching for a value in the second array.
>
> Array
> (
> [0] => Array
> (
> [id] => 6
> [answer] => A
> [remote_address] => 24.15.143.7
> [date_time] => 2006-06-20 07:35:24
> [survey_id] => 1
> )
>
> [1] => Array
> (
> [id] => 4
> [answer] => B
> [remote_address] => 24.15.143.7
> [date_time] => 2006-06-20 07:35:24
> [survey_id] => 1
> )
> )
>
> I'm just not getting how the php example works with usort() or
> array_filter().
>
> Sincerely,
> Mike
> --
> Mike Brandonisio * IT Planning & Support
> Tech One Illustration * Database Applications
> tel (630) 759-9283 * e-Commerce
> [EMAIL PROTECTED] * www.techoneillustration.com
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor --------------------
> ~-->
> Great things are happening at Yahoo! Groups. See the new email
> design.
> http://us.click.yahoo.com/TISQkA/hOaOAA/yQLSAA/HKFolB/TM
> --------------------------------------------------------------------
> ~->
>
> Community email addresses:
> Post message: [email protected]
> Subscribe: [EMAIL PROTECTED]
> Unsubscribe: [EMAIL PROTECTED]
> List owner: [EMAIL PROTECTED]
>
> Shortcut URL to this page:
> http://groups.yahoo.com/group/php-list
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
------------------------ Yahoo! Groups Sponsor --------------------~-->
See what's inside the new Yahoo! Groups email.
http://us.click.yahoo.com/2pRQfA/bOaOAA/yQLSAA/HKFolB/TM
--------------------------------------------------------------------~->
Community email addresses:
Post message: [email protected]
Subscribe: [EMAIL PROTECTED]
Unsubscribe: [EMAIL PROTECTED]
List owner: [EMAIL PROTECTED]
Shortcut URL to this page:
http://groups.yahoo.com/group/php-list
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/php-list/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/