Sort an Array by a Child Array's Value in PHP

Sort an array by a child array's value in PHP

You can make use of usort function as:

$arr = array(
array("105945", "First name", 0.080878465391),
array("109145", "Second name", 0.0504154818384)
);

function cmp($a, $b){
if($a[2] == $b[2]){
return 0;
}
return ($a[2] < $b[2]) ? -1 : 1;
}

usort($arr, "cmp");

Sort array and child arrays by value

If I understand your explanation of the problem correctly, following code will work for you:

//sort the outer array
usort($array, function($a, $b) {
return $a['position'] - $b['position'];
});
//sort childrens
foreach ($array as &$item) {
usort($item['children'], function($a, $b) {
return $a['position'] - $b['position'];
});
}

In any case, usort is a native php function that will be very handy for described case. http://php.net/manual/en/function.usort.php

Sorting an array of arrays by the child array's length?

This will work:

function sort_by_length($arrays) {
$lengths = array_map('count', $arrays);
asort($lengths);
$return = array();
foreach(array_keys($lengths) as $k)
$return[$k] = $arrays[$k];
return $return;
}

Note that this function will preserve the numerical keys. If you want to reset the keys, wrap it in a call to array_values().

Sort dependent child array in php

You can use it in the following way:

$array= Array( 
Array("self"=>"user4", "parent"=>"user6"),
Array("self"=>"user2", "parent"=>"user1"),
Array("self"=>"user1", "parent"=>"user4"),
Array("self"=>"user5", "parent"=>"user2"),
Array("self"=>"user6", "parent"=>"user3"),
Array("self"=>"user3", "parent"=>"Parent") // it will be anything
);
$result = "";
foreach($array as $val)
{
if($val['parent']== 'Parent')
{
$result[] = $val;
getNextNode($val['self']);
}
}
function getNextNode($child)
{
global $array;
global $result;
foreach($array as $val)
{
if($val['parent'] == $child)
{
$result[] = $val;
getNextNode($val['self']);
}
}
}
echo "<pre/>";
print_r($result);

You get the desired result.

sorting array based on child array[0] (unix) value

array_multisort() — Sort multiple or multi-dimensional arrays

array_columns() — Return the values from a single column in the input array

You can use array_multisort() and array_column(), then provide your desired sort order (SORT_ASC or SORT_DESC).

array_multisort(array_column($array, "unix"), SORT_ASC, $array);

Explanation:

In array_multisort(), arrays are sorted by the first array given. You can see we are using array_column($array, "unix"), which means that the second parameter is the order of sorting (ascending or descending) and the third parameter is the original array.

This is the result of array_column($array, "unix"):

Array(
[0] => 1556547761
[1] => 1556547761
[2] => 1556547765
[3] => 1556547769
)

Sorting array by child value

Solutions:

$json = json_decode(file_get_contents("http://www.linktojson.com"), true);

// sorting by comments count
usort($json['location']['media']['nodes'], 'sort_by_comments');

// OR sorting by likes count
usort($json['location']['media']['nodes'], 'sort_by_likes');

// sorting functions:
function sort_by_comments($a, $b) {
return $a['comments']['count'] - $b['comments']['count'];
}

function sort_by_likes($a, $b) {
return $a['likes']['count'] - $b['likes']['count'];
}

PHP, Sort Multidimensional Array by Child Array

Don't use strcmp :)

function custom_sort($a, $b) {
return $a['revenue_certificate'] - $b['revenue_certificate'];
}

usort($data_array, 'custom_sort');

custom_sort should return a negative, 0, positive value when $a < $b, $a == $b, $a < $b respectively (just as strcmp does BTW).

PHP: How to order array by a value of child array

Use usort: If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

function sortByAge($a, $b) {
return $a['age'] - $b['age'];
}

usort($myArray, 'sortByAge');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
return $a['age'] - $b['age'];
});

And finally with PHP 7 you can use the "spaceship operator":

usort($myArray, function($a, $b) {
return $a['age'] <=> $b['age'];
});

Source: Sort Multi-dimensional Array by Value

Sort Parent array by child value

Use uasort, user-defined sorting that lets you provide your own function to define whether one element should come before another.

http://www.php.net/manual/en/function.uasort.php

Your comparator function would be something like

function cmp($a, $b) {
return $a['post_id'] > $b['post_id'] ? 1 : -1;
}


Related Topics



Leave a reply



Submit