I have array:
$array = array(array('2012-12-12', 'vvv'), array('2012-12-14', 'df'),array('2012-12-10', 'vvv'),array('2012-12-11', 'vvv'));
Array
(
[0] => Array
(
[0] => 2012-12-12
[1] => vvv
)
[1] => Array
(
[0] => 2012-12-14
[1] => df
)
[2] => Array
(
[0] => 2012-12-10
[1] => vvv
)
[3] => Array
(
[0] => 2012-12-11
[1] => vvv
)
)
http://codepad.org/gxw2yKMU
is possible to sort this with dates DESC? For this example should be:
$array[1] //2012-12-14
$array[0] //2012-12-12
$array[3] //2012-12-11
$array[2] //2012-12-10
For me the best way is use embedded functions for PHP, but how? :)
You can use usort with a custom function. If you're on PHP < 5.3 you'll need a named function rather than, as I have, an anonymous one.
$array = array(
array('2012-12-12', 'vvv'),
array('2013-12-14', 'df'),
array('2012-12-14', 'df'),
array('2012-12-10', 'vvv'),
array('2012-12-11', 'vvv')
);
usort($array, function($a, $b) {
if ($a[0] == $b[0]) return 0;
return ($a > $b) ? -1 : 1;
});
print_r($array);
You should be able to use usort
usort( $array, 'sortFunction' );
function sortFunction( $a, $b ) {
if( $a[0] == $b[0] )
return 0;
return ( $a[0] > $b[0] ? return -1 : 1 );
}
You can use array_multisort() :
foreach ($array as $key => $row) {
$dates[$key] = $row[0];
}
array_multisort($dates, SORT_DESC, $array);
First, you put out all dates in a new array. Then, array_multisort() will sort the second array ($array) in the same order than the first ($dates)
Related
I have list of unique elements and want to change it to list of associative arrays. What is the most elegant way to do this? I tried foreach but it looks bogus.
Expected Input:
array('2019-10-01', '2019-10-02', '2019-10-03')
Expected Output:
array(array('day' => '2019-10-01'), array('day' => '2019-10-02'), array('day' => '2019-10-03'))
You can use array_map:
$array = array('2019-10-01', '2019-10-02', '2019-10-03');
$output = array_map(function ($v) { return array('day' => $v); }, $array);
or a simple foreach:
$output = array();
foreach ($array as $v) {
$output[] = array('day' => $v);
}
In both cases the output is the same:
Array
(
[0] => Array
(
[day] => 2019-10-01
)
[1] => Array
(
[day] => 2019-10-02
)
[2] => Array
(
[day] => 2019-10-03
)
)
Demo on 3v4l.org
See this short code example. it iterates over the given array and associates a key with an increment:
$a = Array('2019-10-01', '2019-10-02', '2019-10-03');
$b = [];
for($x = 0; $x < count($a); $x++) {
$b['day' . $x] = $a[$x];
}
print_r($b);
// output: Array ( [day0] => 2019-10-01 [day1] => 2019-10-02 [day2] => 2019-10-03 )
$arr['a']['studentname'] = "john";
$arr['b']['studentname'] = "stefen";
$arr['c']['studentname'] = "alex";
is it possible to sort using user defined functions:
usort( $arr )
uasort( $arr )
uksort( $arr )
so based on value which i need to pass, the array should be sorted!
expected output:
if the current value then
Array
(
[c] => Array
(
[studentname] => alex
)
[a] => Array
(
[studentname] => john
)
[b] => Array
(
[studentname] => stefen
)
)
if the current value then
Array
(
[b] => Array
(
[studentname] => stefen
)
[a] => Array
(
[studentname] => john
)
[c] => Array
(
[studentname] => alex
)
)
thanks in advance
If I understood the question, you can use a simple string compare callback:
$arr['a']['studentname'] = "john";
$arr['b']['studentname'] = "stefen";
$arr['c']['studentname'] = "alex";
// A-Z
uasort($arr, function($a, $b) {
return strcmp($a['studentname'], $b['studentname']);
});
print_r($arr);
// Z-A
uasort($arr, function($a, $b) {
return strcmp($b['studentname'], $a['studentname']);
});
print_r($arr);
Try this:
For PHP version > 5.3:
$arr['a']['studentname'] = "john";
$arr['b']['studentname'] = "stefen";
$arr['c']['studentname'] = "alex";
uasort($arr, function($a, $b) {
return strcmp($a['studentname'], $b['studentname']);
});
For PHP version < 5.3:
$arr['a']['studentname'] = "john";
$arr['b']['studentname'] = "stefen";
$arr['c']['studentname'] = "alex";
function sort_by($a, $b) {
return strcmp($a['studentname'], $b['studentname']);
}
uasort($arr, 'sort_by');
Hello how to sort arrays by keys and values too... so if user input this value
$input = array(0,1,0,2,0);
then the result should be like this since they're the same input they should maintain their keys too...
Array
(
[0] => 0
[2] => 0
[4] => 0
[1] => 1
[3] => 2
)
not like this... the keys is jumbled and I really that key to work on my project of FCFS Scheduling.
Array
(
[4] => 0
[0] => 0
[2] => 0
[1] => 1
[3] => 2
)
btw I used asort. someone help me how to fix this?
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(0,1,0,2,0);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo " $value\n";
}
Stable sort would help here. But php don't have any stable sorting functions since 4.1.
But you can use uksort + closure.
$input = array(0,1,0,2,0);
$cmp = function($a, $b) use($input){
if($input[$a] > $input[$b]){return 1;}
elseif($input[$a] < $input[$b]){return -1;}
elseif($a>$b){return 1;}
elseif($a<$b){return -1;}
return 0;
};
uksort($input, $cmp);
print_r($input);
https://eval.in/145923
Or shorter version
$cmp = function($a, $b) use($input){
return (($input[$a]-$input[$b])?:($a-$b));
};
Simple use the sort function
$input = array(0,1,0,2,0);
sort($input);
Result:-
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 1
[4] => 2
)
I am trying to order an array produced by a foreach loop, here is my code:
$lowestvar = array();
foreach ($variations as $variation){
$lowestvar[] = $variation['price_html'];
}
I am then using array_multisort like this:
array_multisort($lowestvar, SORT_ASC);
print_r($lowestvar);
This works for the first looped item with a output of:
Array ( [0] => £10.00 [1] => £15.00 )
But the second array in the loop looks like this:
Array ( [0] => £10.00 [1] => £5.00 )
Any ideas on where i am going wrong?
You're sorting STRINGS, which means that 10 < 5 is true. Remember that string sorting go char-by-char, not by "entire value".
You can use usort() as like in the following example
function cmp($a1, $b1)
{
$a=str_replace('£','',$a1);
$b=str_replace('£','',$b1);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array('£10.00','£5.00');
usort($a, "cmp");
print_r($a);
Output
Array
(
[0] => £5.00
[1] => £10.00
)
How can I sort a 2D array in PHP.
I want to sort on date, Array is in this format :
[result] => Array
(
[0] => Array
(
[link] => http://local/node/0
[date] => 13158505310
)
[1] => Array
(
[link] => http://local/node/1
[date] => 13158505311
)
[2] => Array
(
[link] => http://local/node/2
[date] => 13158505312
Use usort:
usort( $array, function( $a, $b ){ return $a["date"] - $b["date"]; } );
Use this
function sortByDateDesc($a, $b) {
return strcmp($a["date"], $b["date"]);
}
function sortByDateAsc($a, $b) {
if ($a['date'] == $b['date']) {
return 0;
}
return ($a['date'] > $b['date']) ? -1 : 1;
}
usort($array, 'sortByDateDesc'); //Descending order
//usort($array, 'sortByDateAsc'); //Asceding order
Use http://nl.php.net/manual/en/function.usort.php
You could also try multisort http://www.php.net/manual/en/function.array-multisort.php
may be this code helpful to you....
// Obtain a list of columns
foreach (data as key => row) {
links[key] = row['link'];
dates[key] = row['date'];
}
// Sort the data with link descending, date ascending
// Add $data as the last parameter, to sort by the common key
array_multisort(link, SORT_DESC, date, SORT_ASC, data);