[PHP] sorting multi array
hi, I have the following array, which I need to sort by quantity... Array ( [2408] => Array ( [name] => Havaianas Top Pink Crystal [size] => 5 (37/38) [quantity] => 4 ) [3388] => Array ( [name] => Havaianas Brazil Silver [size] => 6/7 (39/40) [quantity] => 6 ) [2666] => Array ( [name] => Havaianas Brasil Black [size] => 8/9 (41/42) [quantity] => 1 ) [3210] => Array ( [name] => Havaianas Margaridas Yellow [size] => 5 (37/38) [quantity] => 1 ) [2552] => Array ( [name] => Havaianas Flash White [size] => 5 (37/38) [quantity] => 1 ) ) I need to keep the indexes if poss. Many thanks, jon -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting multi-array
hello, i have got a problem, tehere is an array: $x = array( array(15,55,array(1,2,3),3,5,array(1,2,5)), array(25,55,array(1,2,3),3,5,array(1,2,5)), array(5,55,array(1,2,3),3,5,array(1,2,5)) ); and I need to sort this arraybz first item of sub-arrays (x[0][0], $x[1][0], $x[2][0]). this is the correct result: $x = array( array(5,55,array(1,2,3),3,5,array(1,2,5)), array(15,55,array(1,2,3),3,5,array(1,2,5)), array(25,55,array(1,2,3),3,5,array(1,2,5)) ); some idea how sort this multidimensional array? thank you for your reply. jiří němec, ICQ: 114651500 www.menea.cz - www stránky a aplikace -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi array
Jon, I would suggest that you should have a look at the function "array_multisort". See the manual for details on what this function is capable of. //frank 25 apr 2007 kl. 01.58 skrev Jon Bennett: hi, I have the following array, which I need to sort by quantity... Array ( [2408] => Array ( [name] => Havaianas Top Pink Crystal [size] => 5 (37/38) [quantity] => 4 ) [3388] => Array ( [name] => Havaianas Brazil Silver [size] => 6/7 (39/40) [quantity] => 6 ) [2666] => Array ( [name] => Havaianas Brasil Black [size] => 8/9 (41/42) [quantity] => 1 ) [3210] => Array ( [name] => Havaianas Margaridas Yellow [size] => 5 (37/38) [quantity] => 1 ) [2552] => Array ( [name] => Havaianas Flash White [size] => 5 (37/38) [quantity] => 1 ) ) I need to keep the indexes if poss. Many thanks, jon -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett -- 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 multi array
> I have the following array, which I need to sort by quantity... > I need to keep the indexes if poss. This may not be the most elegant solution. Let's call your array $origArray $tmpArray = array(); foreach( $origArray as $elKey => $elArray ) { $tmpArray[$elArray['quantity']] = $elKey; } if( ksort( $tmpArray )) { $sortedArray = array(); foreach( $tmpArray as $elKey ) { $sortedArray[$elKey] = $origArray[$elKey]; } echo 'Original: ' . print_r( $origArray, TRUE ) ''; echo 'Sorted: ' . print_r( $sortedArray, TRUE ) ''; } else { echo 'Sorry, couldn\'t sort'; } thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting multi array
2007. 04. 25, szerda keltezéssel 11.39-kor Chris Boget ezt írta: > > I have the following array, which I need to sort by quantity... > > I need to keep the indexes if poss. > > This may not be the most elegant solution. Let's call your array > $origArray > > $tmpArray = array(); > foreach( $origArray as $elKey => $elArray ) { > $tmpArray[$elArray['quantity']] = $elKey; > > } > > if( ksort( $tmpArray )) { > $sortedArray = array(); > foreach( $tmpArray as $elKey ) { > $sortedArray[$elKey] = $origArray[$elKey]; > > } > echo 'Original: ' . print_r( $origArray, TRUE ) ''; > echo 'Sorted: ' . print_r( $sortedArray, TRUE ) ''; > > } else { > echo 'Sorry, couldn\'t sort'; > > } this won't work if he has the same quantity for several keys, I think greets Zoltán Németh > > thnx, > Chris > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi array
this won't work if he has the same quantity for several keys, I think Yes, you are correct. If that is the case, then you would just need to change the following line $tmpArray[$elArray['quantity']] = $elKey; to $tmpArray[$elArray['quantity']][] = $elKey; then change logic in this loop: foreach( $tmpArray as $elKey ) { to handle the additional array. If additional sorting needs to occur for those keys that have the same quantity then I think recursion will be in order. thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi array
array_multisort accepts column arrays but here you try to sort row based arrays. try this: $b[self::$sortMember]; } } ArrayUtility::sortByMember($array, 'quantity'); ?> Am 25.04.2007, 12:36 Uhr, schrieb Frank Arensmeier <[EMAIL PROTECTED]>: Jon, I would suggest that you should have a look at the function "array_multisort". See the manual for details on what this function is capable of. //frank 25 apr 2007 kl. 01.58 skrev Jon Bennett: hi, I have the following array, which I need to sort by quantity... Array ( [2408] => Array ( [name] => Havaianas Top Pink Crystal [size] => 5 (37/38) [quantity] => 4 ) [3388] => Array ( [name] => Havaianas Brazil Silver [size] => 6/7 (39/40) [quantity] => 6 ) [2666] => Array ( [name] => Havaianas Brasil Black [size] => 8/9 (41/42) [quantity] => 1 ) [3210] => Array ( [name] => Havaianas Margaridas Yellow [size] => 5 (37/38) [quantity] => 1 ) [2552] => Array ( [name] => Havaianas Flash White [size] => 5 (37/38) [quantity] => 1 ) ) I need to keep the indexes if poss. Many thanks, jon -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett --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 multi array
Jon Bennett wrote: hi, I have the following array, which I need to sort by quantity... Array ( [2408] => Array ( [name] => Havaianas Top Pink Crystal [size] => 5 (37/38) [quantity] => 4 ) [3388] => Array ( [name] => Havaianas Brazil Silver [size] => 6/7 (39/40) [quantity] => 6 ) [2666] => Array ( [name] => Havaianas Brasil Black [size] => 8/9 (41/42) [quantity] => 1 ) [3210] => Array ( [name] => Havaianas Margaridas Yellow [size] => 5 (37/38) [quantity] => 1 ) [2552] => Array ( [name] => Havaianas Flash White [size] => 5 (37/38) [quantity] => 1 ) ) I need to keep the indexes if poss. Many thanks, jon This works for me: function cmp($a, $b) { $aq = $a['quantity']; $bq = $b['quantity']; if ($a == $b) { return 0; } return ($aq < $bq) ? -1 : 1; } uasort($data, "cmp"); To reverse the sort order: return ($aq > $bq) ? -1 : 1; -- _ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi array
Search the archives for "multisort array" and you should find this thread a few thousand times... On Tue, April 24, 2007 6:58 pm, Jon Bennett wrote: > hi, > > I have the following array, which I need to sort by quantity... > > Array > ( > [2408] => Array > ( > [name] => Havaianas Top Pink Crystal > [size] => 5 (37/38) > [quantity] => 4 > ) > > [3388] => Array > ( > [name] => Havaianas Brazil Silver > [size] => 6/7 (39/40) > [quantity] => 6 > ) > > [2666] => Array > ( > [name] => Havaianas Brasil Black > [size] => 8/9 (41/42) > [quantity] => 1 > ) > > [3210] => Array > ( > [name] => Havaianas Margaridas Yellow > [size] => 5 (37/38) > [quantity] => 1 > ) > > [2552] => Array > ( > [name] => Havaianas Flash White > [size] => 5 (37/38) > [quantity] => 1 > ) > ) > > I need to keep the indexes if poss. > > Many thanks, > > jon > > -- > > > jon bennett > t: +44 (0) 1225 341 039 w: http://www.jben.net/ > iChat (AIM): jbendotnet Skype: jon-bennett > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi-array
www.php.net/usort - slight modification of the example (hint: add [0] to $a and $b). Jiří Němec wrote: hello, i have got a problem, tehere is an array: $x = array( array(15,55,array(1,2,3),3,5,array(1,2,5)), array(25,55,array(1,2,3),3,5,array(1,2,5)), array(5,55,array(1,2,3),3,5,array(1,2,5)) ); and I need to sort this arraybz first item of sub-arrays (x[0][0], $x[1][0], $x[2][0]). this is the correct result: $x = array( array(5,55,array(1,2,3),3,5,array(1,2,5)), array(15,55,array(1,2,3),3,5,array(1,2,5)), array(25,55,array(1,2,3),3,5,array(1,2,5)) ); some idea how sort this multidimensional array? thank you for your reply. jiří němec, ICQ: 114651500 www.menea.cz - www stránky a aplikace -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php