Sort numeric multidimensional array by second column PHP [duplicate] - php

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 10 months ago.
I have this array:
I want to order it from bigger to smaller sorted by the number in the second column.
I can't get to the second one :(
array (size=72)
0 =>
array (size=2)
0 => string 'Australian SPI 200' (length=18)
1 => string '-0.055' (length=6)
1 =>
array (size=2)
0 => string 'CAC 40' (length=6)
1 => string '-0.007' (length=6)
2 =>
array (size=2)
0 => string 'DAX' (length=3)
1 => string '0.007' (length=5)
3 =>
array (size=2)
0 => string 'EuroStoxx50' (length=11)
1 => string '0.000' (length=5)

function sortByOrder($a, $b) {
return $a['1'] - $b['1'];
}
$myArray=array (array ( 'Australian SPI 200' , -0.040 ) , array ( 'CAC 40', -0.006 ) ,array ( 'DAX' ,0.009 ));
usort($myArray, 'sortByOrder');
print_r($myArray);
output
Array ( [0] => Array ( [0] => DAX [1] => 0.009 ) [1] => Array ( [0] => CAC 40 [1] => -0.006 ) [2] => Array ( [0] => Australian SPI 200 [1] => -0.04 ) )

Related

Sort multidimensonal array by version

Does anyone know how to correctly sort this array by version preserving the sub-arrays.
I have seen similar issues and solutions on stackoverflow but im not able to apply to my array.
Thanks in advance
array (size=3)
'1.23.006' =>
array (size=1)
0 => string '1' (length=1)
'2.0.0' =>
array (size=1)
0 => string '1' (length=1)
'10.0.0' =>
array (size=2)
0 => string '1' (length=1)
1 => string '4' (length=1)
I want it to be sorted like this:
array (size=3)
'10.0.0' =>
array (size=2)
0 => string '1' (length=1)
1 => string '4' (length=1)
'2.0.0' =>
array (size=1)
0 => string '1' (length=1)
'1.23.006' =>
array (size=1)
0 => string '1' (length=1)
You need to use uksort to sort your array by keys, with a custom function that calls version_compare to compare them:
$data = array('1.23.006' => array('1'), '2.0.0' => array('1'), '10.0.0' => array('1', '4'));
uksort($data, function ($a, $b) {
return version_compare($b, $a);
});
print_r($data);
Output:
Array
(
[10.0.0] => Array
(
[0] => 1
[1] => 4
)
[2.0.0] => Array
(
[0] => 1
)
[1.23.006] => Array
(
[0] => 1
)
)
Demo on 3v4l.org

How to parse this format?

I'm trying to parse a string which looks like this :
"['x','y','z',['a'],[],[['name','2'],['name','40']]]"
I would like to put everythings in an array like this :
0 => string 'x' (length=1)
1 => string 'y' (length=1)
2 => string 'z' (length=1)
3 =>
array (size=1)
0 => string 'a' (length=1)
4 =>
array (size=0)
empty
5 =>
array (size=2)
0 =>
array (size=2)
0 => string 'name' (length=4)
1 => string '2' (length=1)
1 =>
array (size=2)
0 => string 'name' (length=4)
1 => string '40' (length=2)
Is there a proper way to do it because I tried to make a bunch of functions, but at the end it's always a mess and it doesn't work.
Thats nearly a JSON String, so make it into one and from there its easy running
$nearlyJson = "['x','y','z',['a'],[],[['name','2'],['name','40']]]";
$nowItsJson = str_replace("'", '"', $nearlyJson );
$NowItsAPHPArray = json_decode($nowItsJson );
print_r($NowItsAPHPArray );
RESULT
Array
(
[0] => x
[1] => y
[2] => z
[3] => Array
(
[0] => a
)
[4] => Array
(
)
[5] => Array
(
[0] => Array
(
[0] => name
[1] => 2
)
[1] => Array
(
[0] => name
[1] => 40
)
)
)

sort associative array by key value

I try to sort my array by "delais_livraison" by DESC in php but i dont know how i can do that
In the first level I expected to have in first Etoile => 25 and then Feuillage => 10
Array
(
[A] => Array
(
[0] => Array
(
[Feuillage] => Array
(
[delais_livraison] => 10
)
)
[1] => Array
(
[Étoiles] => Array
(
[delais_livraison] => 25
)
)
)
[B] => Array
(
[0] => Array
(
[Grenouillère] => Array
(
[delais_livraison] => 7
)
)
[1] => Array
(
[Chaussons] => Array
(
[delais_livraison] => 0
)
)
)
)
Edit :
this how my array has been build
pastebin
Just try with:
$data = array(
'A' => array(
array('Feuillage' => array('delais_livraison' => 10)),
array('Étoiles' => array('delais_livraison' => 25)),
),
'B' => array(
array('Grenouillère' => array('delais_livraison' => 7)),
array('Chaussons' => array('delais_livraison' => 0)),
),
);
foreach ($data as &$group) {
usort($group, function($itemA, $itemB){
$a = current($itemA)['delais_livraison'];
$b = current($itemB)['delais_livraison'];
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
});
}
Output:
array (size=2)
'A' =>
array (size=2)
0 =>
array (size=1)
'Étoiles' =>
array (size=1)
'delais_livraison' => int 25
1 =>
array (size=1)
'Feuillage' =>
array (size=1)
'delais_livraison' => int 10
'B' => &
array (size=2)
0 =>
array (size=1)
'Grenouillère' =>
array (size=1)
'delais_livraison' => int 7
1 =>
array (size=1)
'Chaussons' =>
array (size=1)
'delais_livraison' => int 0
Solution uses anonymous function that has been introduced in PHP 5.3.0. For older versions just create a function and specify it in usort function:
function my_sort_function($itemA, $itemB){
// ...
}
usort($group, 'my_sort_function');
Since your array has two levels, how do you want to manage a global sort ?
Do you want to do a sort for each sub array ( A, B, ...) ?
In this case the approach will be to use function uksort for each sub array (A and B) and define your own compare function to compare delais_livraison values.
You would appear to be looking for ksort, or, if you have to supply your own comparison function, uksort.

How to convert directory path to an array in php [duplicate]

This question already has an answer here:
Variable containing a path as a string to multi-dimensional array?
(1 answer)
Closed 8 years ago.
I have a directory structure path like
root/path1/path2/path3
I want this to be
Array(
[0] => "root"
[1] => Array
(
[0] => "path1"
[1] => Array
(
[0] => "path2"
[1] => Array
(
[0] => "path3"
)
)
)
)
Just try with:
$input = 'root/path1/path2/path3';
$output = null;
foreach (array_reverse(explode('/', $input)) as $part) {
$output = $output ? array($part, $output) : array($part);
}
var_dump($output);
Output:
array (size=2)
0 => string 'root' (length=4)
1 =>
array (size=2)
0 => string 'path1' (length=5)
1 =>
array (size=2)
0 => string 'path2' (length=5)
1 =>
array (size=1)
0 => string 'path3' (length=5)

Ranking within Arrays [duplicate]

This question already has answers here:
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
Closed 9 years ago.
I have an array $data as below
[data] => Array
(
[user1] => Array
(
[score] => 10
)
[user2] => Array
(
[score] => 15
)
[user3] => Array
(
[score] => 12
)
[user4] => Array
(
[score] => 1
)
)
I am looking for the Ranking of the array based on Score having maximum score as Rank 1
Output:
user2 -> 1
user3 -> 2
user1 -> 3
user4 -> 4
Any suggestions how this could be achieved?
You can use array_multisort function with descending option to achieve this
$array = array('data' => array('user1' => array('score' => 10 ),
'user2' => array('score' => 15),
'user3' => array('score' => 12),
'user4' => array('score' => 1),
)
);
array_multisort($array['data'], SORT_DESC);
var_dump($array);
This will order your array to
array (size=1)
'data' =>
array (size=4)
'user2' =>
array (size=1)
'score' => int 15
'user3' =>
array (size=1)
'score' => int 12
'user1' =>
array (size=1)
'score' => int 10
'user4' =>
array (size=1)
'score' => int 1
TO sort you array you have to use asort($array) function.

Categories