multidimensional array sort ?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Mon Jan 30, 2012 8:31 am

Hi all ! :D

i have this array filled by multiple for loops:

Code: Select all

$array[$counterA][$counterB]['id_ficheclient']
$array[$counterA][$counterB]['nomservicio']
$array[$counterA][$counterB]['priority']
$array[$counterA][$counterB]['id_plato']
$array[$counterA][$counterB]['plato_ES']
$array[$counterA][$counterB]['plato_FR']
$array[$counterA][$counterB]['plato_EN']
$array[$counterA][$counterB]['mi_especialidad']
$array[$counterA][$counterB]['repertoire']
$array[$counterA][$counterB]['idmunicipio']
$array[$counterA][$counterB]['id_zone']
$array[$counterA][$counterB]['id_cuidad']
$array[$counterA][$counterB]['id_tipo_establecimiento']

i try to sort this array by the key "priority"

but no effect, i can't do it
here is my (one of) test code :

Code: Select all

function sortBy_PACK($a, $b) { return $a['priority'] - $b['priority']; }
usort($array, 'sortBy_PACK');
don't work !

i've been trying with array_multisort, but no effect

i think i'm wrong with syntax



(I've been testing a lots of user function found with goggle, but no one correspond to my array, and i still don't understand how to do it!)


how can i reach a key in a multidimensionnal array ?
like this :

Code: Select all

function sortBy_PACK($a, $b) { return $a[??][??]['priority'] - $b[??][??]['priority']; }
usort($array, 'sortBy_PACK');
or

Code: Select all

function sortBy_PACK($a, $b) { return $a[3]['priority'] - $b[3]['priority']; }
usort($array, 'sortBy_PACK');
(these two examples don't work)

is this structure possible ?? with what i can fill the [??]
should i make a complex loop to reach key "priority" ??



I know i can do this without php sorting function, but it's dirty, slow and make a unnecessary big source code !


help would be appreciated !!

thanks guys ! :)

User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Tue Jan 31, 2012 8:47 am

EXPECTED RESULT :

Code: Select all

$myArray[0][0]['priority']=1;
$myArray[0][1]['priority']=2;
$myArray[0][2]['priority']=3;
$myArray[0][3]['priority']=4;

$myArray[1][0]['priority']=1;
$myArray[1][1]['priority']=2;
$myArray[1][2]['priority']=3;
$myArray[1][3]['priority']=3;
i need to sort this array with the key 'priority'

how can i do this ?

i've been trying to modify this function :

Code: Select all

usort($myArray,'sortBy_PACKII');
function sortBy_PACKII($a,$b){
     return $a[0]['priority']-$b[0]['priority'];
}
[EDIT] I understood after searching that my PHP version is 5.2.14 and this function need PHP ver 5.3

I'm trying to rewrite this function to make it work in PHP v5.2.14

how can I do this ? Or maybe there is other solutions ?

thanks guys

Post Reply