How to sort this array [duplicate] - php

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");

Related

Looping thought multilevel array php [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 years ago.
I have an array in php that looks like this:
print_r($myArray);
Array (
[0] => Array
(
[age] => 1
[time] => 2
[name] => james
[size] => 12
[hieght] => 13
)
[1] => Array
(
[age] => 3
[time] => 1
[name] => tim
[size] => 12
[hieght] => 13
)
[2] => Array
(
[age] => 1
[time] => 2
[name] => john
[size] => 132
[hieght] => 4
)
[3] => Array
(
[age] => 1
[time] => 2
[name] => logan
[size] => 12
[hieght] => 11
)
)
Im trying to loop though every item and save each "size" in a new array. I have looked into solutions for pulling out the "size" from each inner array but cant seem to get it right:
$all_sizes = array();
foreach($myArray as $value) {
foreach($value as $key => $val) {
if($key == "size") {
}
}
}
I am new to PHP so im struggling on the proper syntax for this situation.
Remove the next foreach()
$all_sizes = array();
foreach($myArray as $value => $getSize) {
$all_sizes[] = $getSize['size'];
}
print_r($all_sizes);

Sort array using multiple criteria in PHP [duplicate]

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
)
)

Outputting values from a multi-dimensional array [duplicate]

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.

php | Multidimensional array sorting [duplicate]

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

sort array by key value and display the key [duplicate]

This question already has answers here:
Sorting multidim array: prioritize if column contains substring, then order by a second column
(4 answers)
Sort multidimensional array by multiple columns
(8 answers)
Closed 9 years ago.
I have this Array
Array (
[0] => Array
(
[id] => 61
[testo] => articolo di maggio
[data] => 2013-05-03
[orario] => 00:00:00
[nome_files] => fabmad_1920x1200.jpg
[pubblicato] => 1
)
[1] => Array
(
[id] => 58
[testo] =>
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => 20130603100647_da_installare.rtf
[pubblicato] => 1
)
[2] => Array
(
[id] => 59
[testo] => Demo
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => eye_drops_water_2.jpg
[pubblicato] => 1
)
)
I want to sort it by "data".
I want to display the "data" and for each data the elements...
Try the ksort function or the array-multisort
Check the php documentation: http://php.net/manual/en/function.ksort.php
http://php.net/manual/en/function.array-multisort.php

Categories