1

I'm not sure why I'm having a problem with this but I'm trying to sort an array such as "0,1,0,0,1,1,0,0" so that the number 1's appear at the end, but the array indexes are retained as per their original order.

With the following code example

<pre><?php
  $array = array(0,1,0,0,1,1,0,0);
  print_r($array);
  asort($array);
  print_r($array);
?></pre>

Starting with the original array:

Array
(
    [0] => 0
    [1] => 1
    [2] => 0
    [3] => 0
    [4] => 1
    [5] => 1
    [6] => 0
    [7] => 0
)

After performing the asort($array):

Array
(
    [6] => 0
    [7] => 0
    [0] => 0
    [3] => 0
    [2] => 0
    [1] => 1
    [5] => 1
    [4] => 1
)

But what do I need to do so I can have the following output? (Note index order)

Array
(
    [0] => 0
    [2] => 0
    [3] => 0
    [6] => 0
    [7] => 0
    [1] => 1
    [4] => 1
    [5] => 1
)

I'd realy like to avoid having to do further processing loops to reorder the indexes by distinct value groups (ie sort all the indexes on the items with value "0" then items with value "1" and merge the results)


Edit: this is really messy, but solves what I want to achieve as an example

print_r(stupid_array_order_hack($array));
function stupid_array_order_hack($array) {
  if(isset($array) === TRUE AND is_array($array) === TRUE) {
    $reordering_group = array();
    $reordering_merge = array();
    // Group the index's by value
    foreach($array as $key => $value) {
      $reordering_group[$value][] = $key;
    }
    // sort the grouped index's
    foreach($reordering_group as $key => $value) {
      asort($reordering_group[$key]);
      $reordering_merge = array_merge($reordering_merge,$reordering_group[$key]);
    }
    return array_replace(array_flip($reordering_merge),$array);
  }
  return $array;
}

Solution: Method using array_multisort()

$array = array(0,1,0,0,1,1,0,0);
$temp = array($array,array_keys($array));
array_multisort($temp[0],SORT_ASC,$temp[1],SORT_ASC);
$array = array_combine($temp[1], $temp[0]);
3
  • 1
    One solution could be to add $i*0.01 to each element where i is its index before sorting and then taking floor after sorting. But this seems crass. Commented May 11, 2012 at 1:21
  • My thoughts are similar to sank's except that solution is a little clearer, even though it is slightly "crass". Commented May 11, 2012 at 1:48
  • Cheers guys, I'll keep this in mind, it is a good suggestion. Commented May 11, 2012 at 2:05

3 Answers 3

1

If the keys need to match up then I suggest you use array_multisort(). For example:

$array =        array(0,1,0,0,1,1,0,0);
$other_array =  array(1,2,3,4,5,6,7,8);

array_multisort($array, $other_array);

This will order the two arrays so that the arrays so that the key orders match up.

http://us.php.net/manual/en/function.array-multisort.php

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

2 Comments

I found a user comment in the array_multisort page that achieved what I wanted, I've revised my above question to show it.
I'll give you the accept because it lead me to finding a solution.
1

You'll need to group the array by values and sort the individual groups:

$array = array(0,1,0,0,1,1,0,0);

$groups = array();
foreach ($array as $key => $value) {
    $groups[$value][$key] = $value;
}

ksort($groups);

$array = array();
foreach ($groups as $group) {
    $array += $group;
}

print_r($array);

1 Comment

Yeah, I just finished writing my own (maybe a bit more complex than what you had) but it is looking to be my only option for now.
0

You may want to use array_merge() to reset array keys.

2 Comments

Thanks, but I'm trying to keep the array keys, otherwise I'd use array_values()
I'll explain further, the order of the keys are being used in reference to another array, I'm using this simple array to re-sort a far more complex structure.

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.