xxxxxxxxxx
$numArray = [
['46', '45', '24', '35', '47', '48'],
['47', '23', '37', '44', '48', '49'],
['49', '10', '14', '22', '50', '51'],
['42', '06', '14', '36', '43', '44'],
['67', '18', '19', '29', '68', '69'],
['69', '06', '09', '44', '70', '71']
];
function sortArray(&$arr) {
foreach ($arr as &$subArray) {
$intArray = array_map('intval', $subArray);
sort($intArray, SORT_NUMERIC);
$subArray = array_map(function($num) {
return str_pad($num, 2, '0', STR_PAD_LEFT);
}, $intArray);
}
}
sortArray($numArray);
usort($numArray, function($a, $b) {
return strcmp(implode('', $a), implode('', $b));
});
echo "<pre>";
print_r($numArray);
echo "</pre>";
xxxxxxxxxx
//php 7+
usort($inventory, function ($item1, $item2) {
return $item1['price'] <=> $item2['price'];
});
xxxxxxxxxx
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
//Would output:
c = apple
b = banana
d = lemon
a = orange
xxxxxxxxxx
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
xxxxxxxxxx
$array_temp_id = array_column($companions, 'temp_id');
array_multisort($array_temp_id, SORT_DESC, $companions);
xxxxxxxxxx
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo $val;
}
/*
OUTPUT:
apple
banana
lemon
orange
*/
?>
xxxxxxxxxx
// take an array with some elements
$array = array('a','z','c','b');
// get the size of array
$count = count($array);
echo "<pre>";
// Print array elements before sorting
print_r($array);
for ($i = 0; $i < $count; $i++) {
for ($j = $i + 1; $j < $count; $j++) {
if ($array[$i] > $array[$j]) {
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
echo "Sorted Array:" . "<br/>";
print_r($array);
xxxxxxxxxx
$carsArray = array( "Volvo", "Honda", "Toyota", "BMW");
sort($carsArray);
$no_car = count($carsArray);
for( $x=0; $x<$no_car; $x++ )
{
echo $carsArray[$x];
echo "<br>";
}