Re: [PHP] sorting associative array

2005-01-24 Thread Abdul-Wahid Paterson
Hi,

How about this


  foreach ($data as $key = $row) {
$scores[$key] = $row['scores'];
  }
 array_multisort($scores, SORT_ASC, $data);


Abdul-Wahid


On Mon, 24 Jan 2005 00:39:17 +1100, Jeffery Fernandez
[EMAIL PROTECTED] wrote:
 I have the following multi-dimentional array and I want to sort it by
 the score key value
 
 print_r($data);
 
 Array
 (
 [0] = Array
 (
 [video_id] = 71
 [old_id] = 7854
 [title] = When the fire comes
 [video_copies] = 1
 [running_time] = 48
 [date_published] = 06/02
 [place_published] = Australia
 [abstract] = ABC TV Gives details on many aspects of bushfires: 
 ...
 [library_medium_id] = 5
 [library_type] = 4
 [score] = 6.3310546875
 )
 
 [1] = Array
 (
 [video_id] = 9
 [old_id] = 7792
 [title] = Fire awareness
 [video_copies] = 1
 [running_time] = 15
 [date_published] =
 [place_published] = Victoria
 [abstract] = Safetycare Australia A general video lookin.
 [library_medium_id] = 5
 [library_type] = 4
 [score] = 3.1997931003571
 )
 
 )
 
 any ideas. Thanks
 
 Jeffery
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


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



Re: [PHP] sorting associative array

2005-01-24 Thread Jochem Maas
...

This won't work for me as I have 500+ records to sort based on the 
score key.. looking at jochem's class now. Thanks

which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Thanks jochem, I had a second look at it and I figured it out. I had 
problem implementing it with a class but now I have figured how to use it.

cheers,
Jeffery
ok, does that mean using usort() on your array works? my class is just a
fancy wrapper around usort - and ofcourse it allows you to sort on more than
1 'column' in the second dimension of the array (kind of like a multi-column
sort in an SQL statement). -- the point being that the usort() example Kurt
(I think that was his name) sent would work also.
anyway glad you could use it. :-)

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


Re: [PHP] sorting associative array

2005-01-24 Thread Jeffery Fernandez
Jochem Maas wrote:
...

This won't work for me as I have 500+ records to sort based on the 
score key.. looking at jochem's class now. Thanks


which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Thanks jochem, I had a second look at it and I figured it out. I had 
problem implementing it with a class but now I have figured how to 
use it.

cheers,
Jeffery

ok, does that mean using usort() on your array works? my class is just a
fancy wrapper around usort - and ofcourse it allows you to sort on 
more than
1 'column' in the second dimension of the array (kind of like a 
multi-column
sort in an SQL statement). -- the point being that the usort() example 
Kurt
(I think that was his name) sent would work also.

anyway glad you could use it. :-)

Yes the example sent by Kurt Yoder worked for me. I coudn't work out the 
errors with the class you sent me. I realised it was written for PHP5 in 
mind ?... or maybe I wasn't patient enough to spent time debugging it :-(

 /**
  * This function is to sort a multi-dimentional array. This is a 
call-back function.
  * Replace the value of $this-mArraySortKey with the appropriate key
  * before calling the call-back function
  */
 function sort_array($x, $y)
 {
   if ( $x[$this-mArraySortKey] == $y[$this-mArraySortKey] )
 return 0;
   else if ( $x[$this-mArraySortKey]  $y[$this-mArraySortKey] )
 return 1;
   else
 return -1;
 }

and within my class I am calling the following two lines:
   // Sort the array
   $this-mArraySortKey = 'score';
   usort($this-mSearchData, array( $this, 'sort_array'));
cheers,
Jeffery Fernandez
http://melbourne.ug.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] sorting associative array

2005-01-24 Thread Ford, Mike
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 23 January 2005 22:37, Jeffery Fernandez wrote:

 Kurt Yoder wrote:
 
  Use usort (stealing from php docs):
  
  
  
  function cmp($a['score'], $b['score'])

That should be:

function cmp($a, $b)

  {
 if ($a['score'] == $b['score']) {
 return 0;
 }
 return ($a['score']  $b['score']) ? -1 : 1;
  }
  
  $data = array(...);
  
  usort($data, cmp);
  
  
 This won't work for me as I have 500+ records to sort based on the
 score key..

Sure it will, given the correction above.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

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



Re: [PHP] sorting associative array

2005-01-24 Thread Jochem Maas
Jeffery Fernandez wrote:
Jochem Maas wrote:
...

Yes the example sent by Kurt Yoder worked for me. I coudn't work out the 
errors with the class you sent me. I realised it was written for PHP5 in 
mind ?... or maybe I wasn't patient enough to spent time debugging it :-(

I did change it for php5 (to get rid of E_STRICT warnings IIR)
- but the change was fairly cosmetic:
the class def starts like:
class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys;  //the order in which we want the array to be sorted.
if you change that to:
class MDASort {
var $dataArray; //the array we want to sort.
var $sortKeys;  //the order in which we want the array to be sorted.
then the class should work under php4 as advertised (adding an '' as you
do below in the callback definition wouldn't hurt either).
...
and within my class I am calling the following two lines:
   // Sort the array
   $this-mArraySortKey = 'score';
   usort($this-mSearchData, array( $this, 'sort_array'));

cool, taking apart someone elses code and rewriting it one of the best ways
of learning/understanding IMHO. btw the '' before $this is not strictly 
required,
but it should save you a few cycles :-)

cheers,
Jeffery Fernandez
http://melbourne.ug.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-24 Thread Jeffery Fernandez
Jochem Maas wrote:
Jeffery Fernandez wrote:
Jochem Maas wrote:
...

Yes the example sent by Kurt Yoder worked for me. I coudn't work out 
the errors with the class you sent me. I realised it was written for 
PHP5 in mind ?... or maybe I wasn't patient enough to spent time 
debugging it :-(

I did change it for php5 (to get rid of E_STRICT warnings IIR)
- but the change was fairly cosmetic:
the class def starts like:
class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys;  //the order in which we want the array to be 
sorted.

if you change that to:
class MDASort {
var $dataArray; //the array we want to sort.
var $sortKeys;  //the order in which we want the array to be sorted.
then the class should work under php4 as advertised (adding an '' as you
do below in the callback definition wouldn't hurt either).
...
and within my class I am calling the following two lines:
   // Sort the array
   $this-mArraySortKey = 'score';
   usort($this-mSearchData, array( $this, 'sort_array'));

cool, taking apart someone elses code and rewriting it one of the best 
ways
of learning/understanding IMHO. btw the '' before $this is not 
strictly required,
but it should save you a few cycles :-)

You always learn by re-writing someone elses code ;-)
I had done the changes to the variable declaration and also changed the 
constructor name to be the name of the class and it still gave me 
errors. Perhaps I will test it without any Error reporting on... needs 
some tweaking I guess.

cheers,
Jeffery Fernandez
http://melbourne.ug.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Jochem Maas
Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value
...
any ideas. Thanks
A long time ago I had this problem, came up with the following class (based 
on
someone else's stuff that I found in the comments section of the php manual) to
handle it for me. its probably a rubbish way of doing it (look forward to
some clever bar steward showing us how do it in 3 lines. :-) ) - there are 
usage examples
at the bottom:
?php
/**
 * MDASort.class.php :: simple interface to sort a mutlidimensional array
 *
 * We often have arrays of arrays where the sub-arrays are rows of data
 * (either created or extracted from a database) - this class allows us to
 * easily sort arrays in the base container array by any number of keys found
 * in the sub-arrays. be aware that it is assumed that the array keys found in 
the
 * sub arrays are associative. Also maybe the _sortcmp method could be enhanced 
to
 * allow arbitrary levels of arrays to be sorted: by calling sort() on a level
 * N array also the sortKeys contents would then have to be checked to see if
 * they applied to the current (sub-)array
 *
 * @author  Some guy on PHP comment board. http://www.php.net/usort\
 * @author  Jochem Maas [EMAIL PROTECTED]
 *
 * $Id: MDASort.class.php,v 1.1 2004/08/03 13:38:37 jochem Exp $
 *
 */
/**
 * This file and its contents is not copyrighted;
 * The contents are free to be used by anybody under any conditions.
 */
class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys;  //the order in which we want the array to be sorted.
function __construct()
{
if ($cnt = func_num_args()) {
$args = func_get_args();
if (isset($args[0])) {
$this-setData($args[0]);
}
if (isset($args[1])) {
$this-setSortKeys($args[1]);
}
if (isset($args[2])  $args[2] === true) {
$this-sort();
}
}
}
function _sortcmp($a, $b, $i=0)
{
   $r = strnatcmp($a[$this-sortKeys[$i][0]],$b[$this-sortKeys[$i][0]]);
   if ($this-sortKeys[$i][1] == DESC) $r = $r * -1;
   if($r==0) {
   $i++;
   if ($this-sortKeys[$i]) $r = $this-_sortcmp($a, $b, $i);
   }
   return $r;
}
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}
function setData($dataArray = array())
{
$this-dataArray = $dataArray;
}
function setSortKeys($sortKeys = array())
{
$this-sortKeys = $sortKeys;
}
function getData()
{
return $this-dataArray;
}
function getSortKeys()
{
return $this-sortKeys;
}
}
/* example of usage */
/*
$sorter = new MDASort;
$sorter-setData( array(
   array(name = hank, headsize = small, age = 32),
   array(name = sade, headsize = petit, age = 36),
   array(name = hank, headsize = large, age = 33),
   array(name = sade, headsize = large, age = 32),
   array(name = john, headsize = large, age = 32),
   array(name = hank, headsize = small, age = 36),
   array(name = hank, headsize = small, age = 40)
));
$sorter-setSortKeys( array(
   array('name','ASC'),
   array('headsize','DESC'),
   array('age','ASC'),
));
$sorter-sort();
$sortedArray = $sorter-getData();
*/
/* 2nd example of usage */
/*
$data = array(
   array(name = hank, headsize = small, age = 32),
   array(name = sade, headsize = petit, age = 36),
   array(name = hank, headsize = large, age = 33),
   array(name = sade, headsize = large, age = 32),
   array(name = john, headsize = large, age = 32),
   array(name = hank, headsize = small, age = 36),
   array(name = hank, headsize = small, age = 40)
);
$sort = array(
   array('name','ASC'),
   array('headsize','DESC'),
   array('age','ASC'),
);
$sorter = new MDASort($data, $sort);
$sorter-sort();
$sortedArray = $sorter-getData();
*/
/* 3rd example of usage */
/*
$data = array(
   array(name = hank, headsize = small, age = 32),
   array(name = sade, headsize = petit, age = 36),
   array(name = hank, headsize = large, age = 33),
   array(name = sade, headsize = large, age = 32),
   array(name = john, headsize = large, age = 32),
   array(name = hank, headsize = small, age = 36),
   array(name = hank, headsize = small, age = 40)
);
$sort = array(
   array('name','ASC'),
   array('headsize','DESC'),
   array('age','ASC'),
);
$sorter = new MDASort($data, $sort, true);  // auto sort
$sortedArray = $sorter-getData();
*/
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Kurt Yoder
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

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

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


Re: [PHP] sorting associative array

2005-01-23 Thread Jeffery Fernandez
Kurt Yoder wrote:
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

This won't work for me as I have 500+ records to sort based on the score 
key.. looking at jochem's class now. Thanks

Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

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

--
Kurt Yoder
http://yoderhome.com

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


Re: [PHP] sorting associative array

2005-01-23 Thread Jochem Maas
Jeffery Fernandez wrote:
Kurt Yoder wrote:
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

This won't work for me as I have 500+ records to sort based on the score 
key.. looking at jochem's class now. Thanks
which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

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

--
Kurt Yoder
http://yoderhome.com


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


Re: [PHP] sorting associative array

2005-01-23 Thread Jeffery Fernandez
Jochem Maas wrote:
Jeffery Fernandez wrote:
Kurt Yoder wrote:
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

This won't work for me as I have 500+ records to sort based on the 
score key.. looking at jochem's class now. Thanks

which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Thanks jochem, I had a second look at it and I figured it out. I had 
problem implementing it with a class but now I have figured how to use it.

cheers,
Jeffery

Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it 
by the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = 
Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

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

--
Kurt Yoder
http://yoderhome.com



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