Sorting an Associative Array in PHP

How to sort an array of associative arrays by value of a given key in PHP?

You are right, the function you're looking for is array_multisort().

Here's an example taken straight from the manual and adapted to your case:

$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);

As of PHP 5.5.0 you can use array_column() instead of that foreach:

$price = array_column($inventory, 'price');

array_multisort($price, SORT_DESC, $inventory);

How to sort associative array by its value using PHP?

uasort() function is what you're looking for.

uasort($data, function($a, $b) { return strcasecmp($a['name'], $b['name']); });

You should take a look on usort() and uksort() doc for examples of how user-defined comparison functions works.

How to sort a php associative array to a specific order?

Easy- Just use the arraySort as the assoc key and get the corresponding array / value from the original array,

<?php

$arraySort = [
"break",
"period1",
"period2",
"period3",
"lunch",
"period4",
"period5",
"period6",
"finish"
];

$final_array = [];

foreach($arraySort as $arraySo){
$final_array[$arraySo] = isset($aData[$arraySo]) ? $aData[$arraySo] : [];
}

print_r($final_array);

Output:- https://3v4l.org/4LXvS

Sorting an associative array in PHP

Use usort and supply your own function to do the ordering, e.g.

function cmp($a, $b)
{
return $b['avgSearchVolume'] - $a['avgSearchVolume'];
}

usort($array, "cmp");

How to sort an Array of Associative Arrays by a Value

use array_multisort method

$arr = array(
array('name' => 'John' , 'total' => '33'),
array('name' => 'Robert' , 'total' => '66'),
array('name' => 'John' , 'total' => '22'),
);

$total = array();
foreach ($arr as $key => $row)
{
$total[$key] = $row['total'];
}
array_multisort($total, SORT_DESC, $arr);

Sort an associative array in PHP using usort

Try to use the uasort() function instead:

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function.

How to sort an associative array in php

PHP has a whole bunch of sorting functions.

The one is sounds like you want is asort()

See the PHP manual for the other alternatives such as sort(), ksort(), natsort(), usort(), and a number of other variations. There's also shuffle() to sort randomly.

[EDIT]
Okay, step-by-step to get the highest value out of the array:

asort($row);  //or arsort() for reverse order, if you prefer.
end($row); //positions the array pointer to the last element.
print current($row); //prints "45" because it's the sorted highest value.
print key($row); //prints "c" because it's the key of the hightst sorted value.

There are a whole bunch of other ways to do it as well.

Sort an associative array in php with multiple condition

You're half way there (though you were sorting backwards for membkey based on your example):

function order_by_member_key($a, $b)
{
if ($a['membkey'] == $b['membkey'])
{
// membkey is the same, sort by head
if ($a['head'] == $b['head']) return 0;
return $a['head'] == 'y' ? -1 : 1;
}

// sort the higher membkey first:
return $a['membkey'] < $b['membkey'] ? 1 : -1;
}
usort($details, "order_by_member_key");

Laravel sorting associative arrays

I hope this helps -

$listItem = collect($related)->sortBy('id_question')->toArray();

Sort an array of associative arrays by multiple columns using a combination of ascending, descending, regular, numeric, and natural sorting

I think you have to implement a custom comparison function for this behavior:

function myCmp($a, $b) {
$nameCmp = strnatcasecmp($a['Name'], $b['Name']);
$ageCmp = strnatcasecmp($a['Age'], $b['Age']);
$codeCmp = strnatcasecmp($a['Code'], $b['Code']);

if ($nameCmp != 0) // Names are not equal
return($nameCmp);

// Names are equal, let's compare age

if ($ageCmp != 0) // Age is not equal
return($ageCmp * -1); // Invert it since you want DESC

// Ages are equal, we don't need to compare code, just return the comparison result
return($codeCmp);
}

Then you can call usort($array, 'myCmp'); and should get the desired sorting



Related Topics



Leave a reply



Submit