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
Related
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
)
)
)
I have var_dump() variable $fruit_array as below:
array (size=3)
0 =>
array (size=1)
'fruit_id' => string '2' (length=1)
1 =>
array (size=1)
'fruit_id' => string '1' (length=1)
2 =>
array (size=1)
'fruit_id' => string '3' (length=1)
I need to rename fruit_id to id and convert array string value to integer, example result:
array (size=3)
0 =>
array (size=1)
'id' => int 2
1 =>
array (size=1)
'id' => int 1
2 =>
array (size=1)
'id' => int 3
How can I do that ? thanks
You can do something like this:
<?php
foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
unset($array[$k]['fruit_id']);
}
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 ) )
I have an array like this
Array
(
[name] => Array
(
[0] => img/test240.jpg
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
)
[link] => Array
(
[0] => http://google.com
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
)
[order] => Array
(
[0] => 4
[1] => 1
[2] => 2
[3] => 3
)
)
I need to sort it by order WHICH IS KEY in Multidimensional array. Here is output.
Array
(
[name] => Array
(
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
[0] => img/test240.jpg
)
[link] => Array
(
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
[0] => http://google.com
)
[order] => Array
(
[1] => 1
[2] => 2
[3] => 3
[0] => 4
)
)
In this you can see when order is sorted name and link is also sorted according to the order. How can i do this with php.
You have to use array_map() in conjunction with sort().
If you want to preserve actual element order you have to use asort() instead sort().
Try this code:
$arr = array(
'name' => array(
0 => 'img/test240.jpg',
1 => 'img/cs1.jpg',
2 => 'img/cs2.jpg',
3 => 'img/cs3.jpg',
),
'link' => array(
0 => 'http://google.com',
1 => 'http://google.com',
2 => 'http://facebook.com',
3 => 'http://orkut.com',
),
'order' => array(
0 => 4,
1 => 1,
2 => 2,
3 => 3,
),
);
function mysort($a) {
asort($a);
return $a;
}
$arr = array_map('mysort', $arr);
print_r($arr);
Demo.
Try this, it uses array_multisort:
$array holds:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/test240.jpg' (length=15)
1 => string 'img/cs1.jpg' (length=11)
2 => string 'img/cs2.jpg' (length=11)
3 => string 'img/cs3.jpg' (length=11)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://google.com' (length=17)
2 => string 'http://facebook.com' (length=19)
3 => string 'http://orkut.com' (length=16)
'order' =>
array (size=4)
0 => string '4' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
Code:
$sort = array();
foreach($array as $k) {
foreach($k as $ind=>$val){
$sort['name'][$ind] = $array['name'][$ind];
$sort['link'][$ind] = $array['link'][$ind];
$sort['order'][$ind] = $array['order'][$ind];
}
}
array_multisort($sort['order'], SORT_ASC, $sort['link'], SORT_ASC, $sort['name'], SORT_ASC);
var_dump($sort);
Output:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/cs1.jpg' (length=11)
1 => string 'img/cs2.jpg' (length=11)
2 => string 'img/cs3.jpg' (length=11)
3 => string 'img/test240.jpg' (length=15)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://facebook.com' (length=19)
2 => string 'http://orkut.com' (length=16)
3 => string 'http://google.com' (length=17)
'order' =>
array (size=4)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 => string '4' (length=1)
$this_arr = array(1,2,3,0);
function my_sort_2($arr, $arrangement)
{
$flag = false;
foreach($arr as $key => $val)
{
if(is_array($arr[$key]))
{
$arr[$key] = my_sort_2($arr[$key],$arrangement);
$flag = true;
}
}
if($flag == false && is_array($arr) && is_assoc($arr) === false)
{
$temp = array();
for($i = 0; $i < count($arrangement); $i++)
{
if(isset($arr[$arrangement[$i]]))
{
$temp[$arrangement[$i]] = $arr[$arrangement[$i]];
unset($arr[$arrangement[$i]]);
}
}
//$arr = array_merge($temp,$arr);
$arr = $temp;
}
return $arr;
}
Include this function below to run my own function. Also credit to #Matt Whittingham where i got this code from
function is_assoc($array)
{
$keys = array_keys($array);
return array_keys($keys) !== $keys;
}
Now let's do some sortin'... print_r(my_sort_2($arr,$this_arr)); assuming $arr contains Shan's array.
The output is EXACTLY what you desired.
It'll search for nested array (at least intended) and see if it's in a standard numeric ordered keys (in short, not custom order - yet; and not assoc) then sort it the way you want.
Note: I know my code isn't that probably good, optimized or bug free and that's my second attempt, misunderstanding your requirements first time (see the function name?).
Well after some research i found a simple solution like this
asort($data['order']);
$keys = array_keys($data['order']);
$data['name'] = array_replace(array_flip($keys), $data['name']);
$data['link'] = array_replace(array_flip($keys), $data['link']);
$data['order'] = array_replace(array_flip($keys), $data['order']);
Although i dont want to apply array_replace and array_flip on all the keys but this is done for the time being. I will surely trying to find how i can do it with single instruction.
I have 2 arrays to compare their indexes, how can i compare them by checking the empty indexes of first array and if so, then change the value of second array of same index.
it can be solved as 2D array by comparing the rows indexes.
array (size=5)
0 => string '' (length=0)
1 => string '10' (length=6)
2 => string '' (length=0)
3 => string '11' (length=3)
4 => string '' (length=0)
array (size=5)
0 => string '1' (length=4)
1 => string '2' (length=11)
2 => string '3' (length=11)
3 => string '4' (length=11)
4 => string '5' (length=10)
For instance, index 0 of first array is empty, so index 0 of second array incremented by 1. and so on
try this
<?php
$a=array('',10,11,'');
$b=array(1,2,3,4,5);
foreach($a as $k=>$v){
if($v==''){
$b[$k]=++$b[$k] ;
}
}
print_r($b);
?>
output
Array ( [0] => 2 [1] => 2 [2] => 3 [3] => 5 [4] => 5 )