Here's my code.
<?php
$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");
foreach ($data as $row) {
foreach ($row as $k){
foreach ($k as $key => $value){
${$key}[] = $value;
}
}
}
array_multisort($pkg_version, SORT_DESC, $data);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
I am trying to sort a multidimensional array using the array_multisort function
I would like to sort the pkg_version of each element to be ordered
The returned order is not as expected. Not sure, I have misunderstood how
array_multisort works? or my code is wrong. could you guys help me? I try to
solve this problem for a long time. it's quite a complex dimension array.
Here's the result after running the code above.
Array
(
[test1] => Array
(
[0] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 2.5.0
)
[1] => Array
(
[toal] => 67
[edition] => 2
[pkg_version] => 0.1.0
)
[2] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 0.3.0
)
)
[test2] => Array
(
[0] => Array
(
[total] => 86
[edition] => 1
[pkg_version] => 1.5.0
)
[1] => Array
(
[total] => 85
[type] => 2
[pkg_version] => 0.53.0
)
[2] => Array
(
[total] => 98
[type] => 2
[pkg_version] => 0.3
)
[3] => Array
(
[total] => 98
[edition] => 2
[pkg_version] => 0.2
)
)
[test3] => Array
(
[0] => Array
(
[total] => 60
[edition] => 6
[pkg_version] => 0.3
)
[1] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.1.1
)
[2] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.25
)
)
)
This is what I expected.
Array
(
[test1] => Array
(
[0] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 2.5.0
)
[1] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 0.3.0
)
[2] => Array
(
[toal] => 67
[edition] => 2
[pkg_version] => 0.1.0
)
)
[test2] => Array
(
[0] => Array
(
[total] => 86
[edition] => 1
[pkg_version] => 1.5.0
)
[1] => Array
(
[total] => 85
[type] => 2
[pkg_version] => 0.53.0
)
[2] => Array
(
[total] => 98
[type] => 2
[pkg_version] => 0.3
)
[3] => Array
(
[total] => 98
[edition] => 2
[pkg_version] => 0.2
)
)
[test3] => Array
(
[0] => Array
(
[total] => 60
[edition] => 6
[pkg_version] => 0.3
)
[1] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.25
)
[2] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.1.1
)
)
)
You can loop over the array and use
usort()This outputs :
Note that in the last array, the version
0.3is higher than the version0.25. Since it's the order in your expected output, I left it as is, but if not, you can use instead ofstrcmp(),version_compare(), this will provide this output for$data['test3']: