Jump to content

how to sort multi array to make a single array?


naeembhatti

Recommended Posts

hi guys how are u i am in trouble plz help me, my prob is.

i have 3 arrays

A = Array ( [0] => 2 [1] => 1 [2] => 3 [3] => 4 [4] => 5 [5] => 7 [6] => 6 )
B=  Array ( [0] => http://localhost/script/images/children/img1.jpg [1] => http://localhost/script/images/children/img3.jpg [2] => http://localhost/script/images/children/img2.jpg [3] => http://localhost/script/images/children/img4.jpg [4] => http://localhost/script/images/children/img10.jpg [5] => http://localhost/script/images/children/img10.jpg [6] => http://localhost/script/images/children/img10.jpg )
C=  Array ( [0] => http://localhost/script/01 [1] => http://localhost/script/03 [2] => http://localhost/script/! [3] => http://localhost/script/04 [4] => http://localhost/script/10 [5] => http://localhost/script/a [6] => http://localhost/script/description )
======================
array(A)-> defines the sort order
array(B)-> should be as sort order of array(A)
array(C)-> should be as sort order of array(A)

actually array(A) is a main array which contains the sort order like in [0]=>2 , [1]=>1
i want so make a single array having sort order of array(A)

like if array(A) have its 1st element to 2 then i want to pick 2nd element from array(B) and from array(C)
i want to make a double dimension array
Ok, asort($a) sorts $a and keeps the index association. (If you used regular sort($a) you wouldn't see any difference because while it would sort your array, it would also reindex it.)

The foreach($a as $key=>$val) just iterates thru every element of your $a array. (We can't use for($i=0; $i<count;$i++) because you'd lose your sort order.) As it goes thru each one, it assigns the key to the $key variable and value to the $val variable. In this case we really don't care about the $val, we really want to get $key because that will tell us which elements of $b and $c to pull next.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.