Suppose I have several arrays.
$a = array("E", "A", NULL, "D", "C");
$b = array("Dog","Cat","Horse","Bear","Zebra");
$c = array(12, 11, 20, 30, 19);
First array not necessary numeric and can contain nulls.
I want to sort all 3 array by the order of first, i.e. to get
"A", "C", "D", "E", NULL
"Cat", "Zebra", "Bear", "Dog", "Horse"
11, 19, 30, 12, 20
I.e. tuples ("E", "Dog", 12), ("A", "Cat", 11), (NULL, "Horse", 20), ("D", "Bear", 30), ("C", "Zebra", 19) should be conserved.
I don't care what happens with null cases: thay can stay in place or go to end or beginning.
You can used array_multisort for sorting multiple array based on first array. See below example
$a = array("E", "A", NULL, "D", "C");
$b = array("Dog","Cat","Horse","Bear","Zebra");
$c = array(12, 11, 20, 30, 19);
array_multisort($a, SORT_ASC, SORT_STRING, $b, $c); // $b and $c sorting based on $a
echo "<pre>";
print_r($b);
print_r($c);
Hope my post will help you out.. Here we are using two function array_combine and ksort. array_combine will combine values with the keys and ksort will sort an array by key
<?php
ini_set('display_errors', 1);
$a = array(5, 1, 2, 4, 3);
$b = array("Dog","Cat","Horse","Bear","Zebra");
$combined=array_combine($a, $b);
ksort($combined);
print_r($combined);
Output:
Array
(
[1] => Cat
[2] => Horse
[3] => Zebra
[4] => Bear
[5] => Dog
)
you can make array like
$array = array(
'1'=>"Cat",
'2' => 'Horse',
'3' => 'Zebra',
'4' => 'Bear',
'5' => 'Dog',
);
This is achieved using array_combine($a, $b) After that, you can use ksort on the combined array.