This question already has answers here:
Sorting a multidimensional array in PHP? [duplicate]
(3 answers)
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 9 years ago.
I have an array and need to be sorted (based on id):
Array
(
[0] => Array
(
[qty] => 1
[id] => 3
[name] => Name1
[sku] => Model 1
[options] =>
[price] => 100.00
)
[1] => Array
(
[qty] => 2
[id] => 1
[name] => Name2
[sku] => Model 1
[options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>.
[price] => 209.00
)
)
Is it possible to sort my array to get output (id based)?
Array
(
[0] => Array
(
[qty] => 2
[id] => 1
[name] => Name2
[sku] => Model 1
[options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>.
[price] => 209.00
)
[1] => Array
(
[qty] => 1
[id] => 3
[name] => Name1
[sku] => Model 1
[options] =>
[price] => 100.00
)
)
Thanks!
Try like
$id_arr = array();
foreach ($my_arr as $key => $value)
{
$id_arr[$key] = $value['id'];
}
array_multisort($id_arr, SORT_DESC, $my_arr);
You can also place SORT_ASC for assending order.Better you add ORDER BY id to the query through which you are getting this array of results
function cmp($a, $b) {
return $a["id"] - $b["id"];
}
usort($arr, "cmp");//$arr is the array to sort
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 8 days ago.
I have an array that I have run json_decode on it...
How can I echo the category_name Hoops from $myarray?
The following is the output of
echo"<pre>;
print_r($myarray);
echo"</pre>;
Array
(
[Althetic] => Array
(
[0] => Array
(
[category_id] => 1
[category_name] => Balls
[parent_id] => 0
)
[1] => Array
(
[category_id] => 2
[category_name] => Hoops
[parent_id] => 0
)
[2] => Array
(
[category_id] => 3
[category_name] => Strings
[parent_id] => 0
)
)
[rawRequest] => Hello world
[hasError] => 1
[errorMessage] => OK
)
I tried
echo $myarray[Althetic][1]->[category_name];
Without any results :(
T
This is a multidimensional array, -> is used to access elements of object.
$myarray['Althetic'][1]['category_name'];
This question already has answers here:
Get object with the highest property value in PHP array
(3 answers)
Find highest value in multidimensional array [duplicate]
(9 answers)
How can I sort arrays and data in PHP?
(14 answers)
Closed 2 years ago.
I have an array in this format:
Array
(
[0] => Array
(
[id] => 117
[name] => Apple
[amount] => 300
)
[1] => Array
(
[id] => 188
[name] => Orange
[count] => 20
)
[2] => Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)
)
I'm trying to get the id of max amount from the associative array.
how can i perform this?
i'm expecting the result
Array
(
[2] => Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)
)
It's simplest to just initialise a "maxkey" value with 0 and then iterate over the array, replacing the key when you find a value with a higher amount:
$maxkey = 0;
foreach ($data as $key => $value) {
if ($value['amount'] > $data[$maxkey]['amount']) {
$maxkey = $key;
}
}
print_r($maxkey);
print_r($data[$maxkey]);
Output:
2
Array
(
[id] => 189
[name] => Grapes
[amount] => 7000
)
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I know there are some other topics about sorting with multiple criteria, but they don't fix my problem.
Let's say I have this array:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
I want to sort it, putting the one with the HIGHEST score on top. On same score, I want the one with the LOWEST endgame number on top.
The sorting mechanisme should rank user1 on top, then user2, then 4 and then user3.
I use this sorting mechanisme:
function order_by_score_endgame($a, $b)
{
if ($a['score'] == $b['score'])
{
// score is the same, sort by endgame
if ($a['endgame'] == $b['endgame']) return 0;
return $a['endgame'] == 'y' ? -1 : 1;
}
// sort the higher score first:
return $a['score'] < $b['score'] ? 1 : -1;
}
usort($dummy, "order_by_score_endgame");
This gives me the following array:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[2] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
As you can see, the array isn't sorted properly... Anyone knows what I'm doing wrong? Thanks a lot!
Your function should be like this:
function order_by_score_endgame($a, $b) {
if ($a['score'] == $b['score']) {
// score is the same, sort by endgame
if ($a['endgame'] > $b['endgame']) {
return 1;
}
}
// sort the higher score first:
return $a['score'] < $b['score'] ? 1 : -1;
}
Try it out. It will give you result like this:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
[3] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
)
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 9 years ago.
I have an array like this
Array
(
[73] => Array
(
[id] => 73
[firstName] => Laura
[lastName] => ...
[email] => ...
[password] => 6d1d3a1dcb9e44eb43605f8ad3c529dd7271749c
[venueId] => 8
[departmentId] => 2
[active] => 1
)
[116] => Array
(
[id] => 116
[firstName] => Rachael
[lastName] => ...
[email] => ...
[password] => 33d83a16aa038e775709fc8d499fe608ad2f4afe
[venueId] => 24
[departmentId] => 1
[active] => 1
)
...etc
I want to sort it so the firstName's are in alphabetical order. Is there a prebuilt PHP function I could use, or would I have to sort it manually?
Use usort()
function cmp($a, $b) {
if ($a['firstName'] == $b['firstName']) {
return 0;
}
return ($a['firstName'] < $b['firstName']) ? -1 : 1;
}
usort($array, "cmp");
This question already has answers here:
How do I retrieve results as multidimensional array from mySQL and PHP?
(4 answers)
Closed 9 years ago.
$this->load->library('opencloud');
$opencloud = new Opencloud;
$containers = $this->opencloud->list_containers();
print_r($containers);
The above code outputs the following array:
Array ( [0] => Array ( [name] => .CDN_ACCESS_LOGS [count] => 2 [bytes]
=> 606 ) [1] => Array ( [name] => Michael Grigsby [count] => 9 [bytes] => 891976 ) [2] => Array ( [name] => Random Photos [count] => 0 [bytes] => 0 ) [3] => Array ( [name] => hello [count] => 10 [bytes] =>
1129257 ) [4] => Array ( [name] => hello_world [count] => 1 [bytes] =>
659737 ) [5] => Array ( [name] => hi [count] => 0 [bytes] => 0 ) )
When I echo out $containers[1]['name'] I get: Michael Grigsby. My question is how do I get the script to output all of the name values rather than simply one?
You can echo the name of each array in this classic mode in PHP with a foreach loop like this:
foreach($containers as $container){
echo($container['name']);
}
<?php
foreach($mainarray as $onearray){
echo $onearray['name'];
}
?>
You can loop around the array with for loop in the following way:
for ($i = 0; $i < count($containers); $i++) {
echo $containers[$i]['name'];
}
Alternatively, you can use a foreach loop to directly get the content without any manipulation of array indexes.