-1

I have the following array:

array
(
[0]=>array
        (
            [0]=>array
            (
                  [value]=>3.5
                  [id] =>1
                  )
                 [1]=>array
                 (
                  [value]=>1.5
                  [id] =>2
                  )
                 [2]=>array
                 (
                  [value]=>1.5
                  [id] =>3
                  )
            )
        )
[1]=>array
        (
            [0]=>array
            (
                  [value]=>4.5
                  [id] =>1
                  )
                 [1]=>array
                 (
                  [value]=>5.5
                  [id] =>2
                  )
                 [2]=>array
                 (
                  [value]=>6.5
                  [id] =>3
                  )
            )
        )
)

I want to sort the array by value where the id is equal, what is the best way to do this?

5
  • Altering the top-level structure, moving elements between top level arrays? How does your sort need to function in the higher dimension? Commented Jul 14, 2011 at 14:22
  • Yes, I want to sort the top level, by values where the id equal. Commented Jul 14, 2011 at 14:23
  • Top-level has multiple values, how to handle that? Commented Jul 14, 2011 at 14:26
  • For example sort by values where the ids equal 1 Commented Jul 14, 2011 at 14:41
  • I still do not have a clear idea of how you are defining this sort. Are you saying to treat it as a flat list, or retain the structure. If you are retaining the structure what dictates if one top level is before or after another. You keep saying "where the id equal", sorting is about ordering, < = >, not just equality. Commented Jul 14, 2011 at 14:43

1 Answer 1

1

Trying my luck…

<?php

$data = array(
    array( /* #1 */
        array('value' => 3.5, 'id' => 1),
        array('value' => 1.5, 'id' => 2),
        array('value' => 1.5, 'id' => 3)
    ),
    array( /* #2 */
        array('value' => 4.5, 'id' => 1),
        array('value' => 5.5, 'id' => 2),
        array('value' => 6.5, 'id' => 3)
    )
);

/* Merge #1 and #2 into one array */
$tmp = array();
foreach ($data as $firstDimension) {
    $tmp = array_merge($tmp, $firstDimension);
}

/* Sort by id (1st, ASC) and value (2nd, DESC) */
usort($tmp, function ($a, $b) {
    $d = $a['id'] - $b['id'];

    if ($d !== 0) {
        return $d;
    }

    return $b['value'] - $a['value'];
});

/* Group entries by id */
$grouped = array();
$c = 0;
foreach ($tmp as $entry) {
    if ($c > 0 && $grouped[$c - 1]['id'] === $entry['id']) {
        $grouped[$c - 1]['values'][] = $entry['value'];
    } else {
        $grouped[] = array('id'     => $entry['id'],
                           'values' => array($entry['value']));
        $c++;
    }
}
unset($tmp);

print_r($grouped);

Outputs:

Array
(
    [0] => Array
        (
            [id] => 1
            [values] => Array
                (
                    [0] => 4.5
                    [1] => 3.5
                )

        )

    [1] => Array
        (
            [id] => 2
            [values] => Array
                (
                    [0] => 5.5
                    [1] => 1.5
                )

        )
[...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.