Jump to content

Function to sort multidimensional array by a value


doddsey_65

Recommended Posts

Sorry for being in the wrong forum but I couldn't post in the Code Snippets section.

 

Here is a quick function that will sort a multidimensional array by a given value

function subval_sort(array $array, $subkey, $reverse = false)
{
	if (empty($array))
	{
		return array();
	}

	$temp_array = array();

	foreach ($array as $key => $value)
	{
		$temp_array[$key] = strtolower($value[$subkey]);
	}
		
	if ($reverse)
	{
		arsort($temp_array);
	}
	else
	{
		asort($temp_array);	
	}

	$_array = array();

	foreach ($temp_array as $key => $value)
	{
		$_array[] = $array[$key];
	}
	
	return $_array;
}

It can be used as such:

$array = [
    0 => [
        'value' => 6
    ],
    1 => [
        'value' => 2
    ],
    2 => [
        'value' => 1
    ]
]
$array = subval_sort($array, 'value');

This would return:

[
    0 => [
        'value' => 1
    ],
    1 => [
        'value' => 2
    ],
    2 => [
        'value' => 6
    ]
]

And it can be reversed by specifying a third parameter (bool)

$array = subval_sort($array, 'value', true);
Link to comment
Share on other sites

Your not returning anything, you're sorting a multidimensional array.

 

The following works perfectly fine:

<?php

$students = array(
	256 => array('name' => 'Jon', 'score' => 98.5),
	2 => array('name' => 'Vance', 'score' => 85.1),
	9 => array('name' => 'Stephen', 'score' => 94.0),
	364 => array('name' => 'Steve', 'score' => 85.1),
	68 => array('name' => 'Rob', 'score' => 74.6)
);

function score_sort($x, $y) {
	return ($x['score'] < $y['score']);
}

// Print the array as is:
echo '<h2>Array As Is</h2><pre>' . print_r($students, 1) . '</pre>';

// Sort by score:
uasort($students, 'score_sort');
echo '<h2>Array Sorted By Score</h2><pre>' . print_r($students, 1) . '</pre>';

Edited by Strider64
Link to comment
Share on other sites

Your not returning anything, you're sorting a multidimensional array.

Yes, your sort function does return something, hence the return keyword. What was being pointed out to you is that using < returns a true/false boolean, but what the function is supposed to return is a a number <0, =0, or >0.

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

The following works perfectly fine:

Just by luck. The code is still incorrect.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.