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.
Related
This question already has answers here:
PHP Add element to every sub array of multi dimension array
(3 answers)
Closed 2 years ago.
I have the following array:
Array (
[0] => Array (
'id' => ...
'name' ...
),
[1] => Array (
'id' => ...
'name' ...
),
[2] => Array (
'id' => ...
'name' ...
),
[3] => Array (
'id' => ...
'name' ...
),
)
What I want to do is to append the same pid key-value pair to every subarray. Is there an easy way to do this without having to do a foreach?
without having to explicitly use a foreach or some other kind of loop, you can use PHP's array_map function. although this will perform an iterated loop under the hood, it will return an array with each element processed by your function.
<?php
$arr = Array (
0 => Array (
'id' => 1,
'name' => 'one'
),
1 => Array (
'id' => 2,
'name' => 'two'
),
2 => Array (
'id' => 3,
'name' => 'three'
),
3 => Array (
'id' => 4,
'name' => 'four'
)
);
function append_pid_kvpair($n){
$n ['pid']= 'value';
return $n;
}
$arr = array_map("append_pid_kvpair",$arr);
echo "<pre>".print_r($arr,true)."</pre>";
will result in this output
Array
(
[0] => Array
(
[id] => 1
[name] => one
[pid] => value
)
[1] => Array
(
[id] => 2
[name] => two
[pid] => value
)
[2] => Array
(
[id] => 3
[name] => three
[pid] => value
)
[3] => Array
(
[id] => 4
[name] => four
[pid] => value
)
)
Hello stackoverflow community. I need help with arrays. It's my weakness. I've got this kind of array:
Array
(
[0] => Array
(
[id] => 7
[slot] => 1
[name] => Apple
[start_date] => 12/16/2015
[end_date] => 03/10/2016
[status] => 1
[pre_exp_email] => 0
)
[1] => Array
(
[id] => 8
[slot] => 1
[name] => Cherry
[start_date] => 12/29/2015
[end_date] => 03/20/2016
[status] => 1
[pre_exp_email] => 0
)
[2] => Array
(
[id] => 5
[slot] => 3
[name] => Bananna
[start_date] => 11/30/2015
[end_date] => 00/00/0000
[status] => 1
[pre_exp_email] => 0
)
[3] => Array
(
[id] => 1
[slot] => 4
[name] => Kiwi
[start_date] => 11/21/2015
[end_date] => 12/21/2016
[status] => 1
[pre_exp_email] => 0
)
)
And my job is to randomize elements which has same [slot], but leave order ascending. For example now it is:
1 Apple 1 Cherry 3 Bannana 4 Kiwi
I need to randomize those elements who has same slot number. So Apple and Cherry would swap positions. How can I do this stuff?
Update : Using shuffle & usort :
shuffle($fruits);
function cmp($a, $b) {
if ($a['slot'] == $b['slot']) {
return 0;
}
return ($a['slot'] < $b['slot']) ? -1 : 1;
}
usort($fruits, "cmp");
Make a new array from the original having slot as keys
$elements = array(
0 => Array
(
'id' => 7,
'slot' => 1
),
1 => Array
(
'id' => 8,
'slot' => 1
),
2 => Array
(
'id' => 9,
'slot' => 1
),
3 => Array
(
'id' => 9,
'slot' => 5
)
);
foreach($elements as $element){
$newArray[$element['slot']][] = $element; //put every element having the same slot
}
$elementSlots = array_keys($newArray); // all slots are stored in elementSlots
$Result = array();
foreach($elementSlots as $slot) {
shuffle($newArray[$slot]); //randomize elements having the same slot
foreach($newArray[$slot] as $element) { //add them to the result array
$Result[$slot][] = $element;//For output 1
//$Result[] = $element; //For output 2
}
}
var_dump($Result);
Output 1:
array (size=2)
1 =>
array (size=3)
0 =>
array (size=2)
'id' => int 7
'slot' => int 1
1 =>
array (size=2)
'id' => int 9
'slot' => int 1
2 =>
array (size=2)
'id' => int 8
'slot' => int 1
5 =>
array (size=1)
0 =>
array (size=2)
'id' => int 9
'slot' => int 5
Output 2:
array (size=4)
0 =>
array (size=2)
'id' => int 7
'slot' => int 1
1 =>
array (size=2)
'id' => int 9
'slot' => int 1
2 =>
array (size=2)
'id' => int 8
'slot' => int 1
3 =>
array (size=2)
'id' => int 9
'slot' => int 5
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 a dummy array, that I want to order.
How can I have following result
with foreach?
with while next()
with RecursiveIterator
with IteratorIterator
which one is the fastest?
Here is array
$files = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
2015 => 'de.php',
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
);
Expected Result
$expected = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'test.php',
4 => 'wp-config.php',
5 => 'wp-content/',
6 => 'wp-content/uploads/',
7 => 'wp-content/uploads/2013/',
8 => 'wp-content/uploads/2013/05/',
9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
14 => '...'
);
array_walk_recursive is what you're looking for.
Example (not tested):
$expected = [];
array_walk_recursive($your_array, function($item, $key) {
// push item on to $expected
$expected[] = $item;
});
Working example
i have two arrays and i need to extract the values of the 2nd array depending on the value of $arr[0]["num"]
$arr = array(
0 => array(
"id" => 24,
"num" => 2
),
1 => array(
"id" => 25,
"num" => 5
)
2 => array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
}
}
is it possible to remove the values of the 2nd array and transfer it into a new array?
what my loop does is just copying the values from the start after each loop. i want to remove the copied values from the 2nd array.
The output should be like this:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
I'd suggest using array_shift
$arr = array(
array(
"id" => 24,
"num" => 2
),
array(
"id" => 25,
"num" => 5
),
array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[0]; // *1
array_shift($array);
}
}
echo '<pre>';
print_r($new);
*1 You have to change this line as well. Since array_shift removes the first array entry, each iteration should access array[0].
Output:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
Try this
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
// unset previous values, in first iteration it will remove 0, 1
unset($array[$i]);
}
// reset the array keys, so for loop $i will start from 0
$array = array_values($array);
}
Output:
array (size=3)
24 =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
25 =>
array (size=5)
0 => string '3' (length=1)
1 => string '4' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
4 => string '7' (length=1)
26 =>
array (size=3)
0 => string '8' (length=1)
1 => string '9' (length=1)
2 => string '10' (length=2)