3

Possible Duplicate:
Sort multidimensional Array by Value (2)

How can i sort array by specific key inside it:

array(
  array(5, 2),
  array(5, 3),
  array(3, 1),
  array(5, 4)
);

It's an array that has several arrays with 2 values, how do i sort by the second value of each array, so result would be :

array(
  array(5, 1),
  array(5, 2),
  array(3, 3),
  array(5, 4)
);
Community
  • 1
  • 1
Osa
  • 1,922
  • 7
  • 30
  • 51
  • I hope this helps you but take a look at [How do I sort a multidimensional array in php](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – BudwiseЯ Oct 13 '12 at 22:51
  • This question could stand to be worded better. It got closed as a duplicate because the differences between it and the question listed are not easily discernable. – cHao Oct 15 '12 at 17:57

3 Answers3

2

Here's one way of doing it:

<?php

$input = array(
    array(5, 2),
    array(5, 3),
    array(3, 1),
    array(5, 4)
);

/**
 * Funkily sort the input array.
 * 
 * @param array $array
 *
 * @return array
 */
function funky_sort(array $array) {
    //Get the array of the first elements
    $first_elements = array_map(function($el) {
        return $el[0];
    }, $array);

    //Get the array of the second elements
    $second_elements = array_map(function($el) {
        return $el[1];
    }, $array);

    //Sort the second elements only
    sort($second_elements, SORT_NUMERIC);

    //Combine both arrays to the same format as the original
    $result = array();
    for ($i = 0; $i < count($first_elements); $i++) {
        $result[] = array($first_elements[$i], $second_elements[$i]);
    }

    //Fire away
    return $result;

}

var_dump(funky_sort($input));
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

Here are some directions which won't solve your problem (the sort is strange) but can lead you to the solution. You can use the usort function ( http://www.php.net/manual/en/function.usort.php ) and define your own comparator.

If you know your sorting logic, add it to the cmp function, which returns 0, if the two elements are equal, -1 if $a < $b and 1 otherwise.

The code will look something like this:

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

$a = array(
  array(5, 2),
  array(5, 3),
  array(3, 1),
  array(5, 4)
);

usort($a, "cmp");
Radoslav Georgiev
  • 1,366
  • 8
  • 15
  • 1
    Your first output would be `array(3, 1)` not `array(5, 1)` as requsted – Baba Oct 13 '12 at 22:36
  • Thanks for pointing that out :) I am giving directions for his problem so he can define the cmp function however he needs – Radoslav Georgiev Oct 13 '12 at 22:39
  • had that to your answer before people starts down voting you ... – Baba Oct 13 '12 at 22:39
  • @RadoslavGeorgiev: Problem is, it's not the right direction. Take a good look at the question. It's not your everyday "sort this array for me" question. – Madara's Ghost Oct 13 '12 at 22:39
  • Updated answer @Baba, thanks for the advcie :) Madara, actually it is. The sorting logic is up to the guy who is asking the question and if he knows where to put it - it's all done. – Radoslav Georgiev Oct 13 '12 at 22:43
  • Oh well, guess that helped. Anyway, thanks for the advice once again :) – Radoslav Georgiev Oct 13 '12 at 22:45
  • @Baba it doesnt matter, i just wanted the second value to be the target, this is the perfect answer, i found it before u post though, also found.. to make it from high to low > change -1 : 1 to 1 : -1 – Osa Oct 13 '12 at 22:51
  • 1
    @Osa: I thought you didn't want to change the positions of the first set of elements. Either I'm confused, or you are, and the latter is more likely. – Madara's Ghost Oct 13 '12 at 22:54
-1
<?php

function sort_by_second($randomArray)
{
    $tmparray1=array();
    $tmparray2=array();
    foreach ($randomArray as $key=>&$ra) {
        $tmparray1[$key]=$ra[1];
        $tmparray2[$key]=$ra[0];
    }
    asort($tmparray1);

    $rr=array();
    foreach ($tmparray1 as $key=>&$val) {
        $rr[$key]=array($tmparray2[$key],$val);
    }
    return $rr;}


$myArray = array(
    array(5, 2),
    array(5, 3),
    array(3, 1),
    array(5, 4)
);


$tt=sort_by_second($myArray);
print_r($tt);

?>
TheodoreV
  • 304
  • 4
  • 17