0

So currently I'm having a problem where I have an array setup like this

array(8) { 
    [0]=> array(2) { 
        [0]=> float(2.1166666666667) 
        [1]=> string(7) "9434493" 
    } 
    [1]=> array(2) { 
        [0]=> float(2.07) 
        [1]=> string(7) "8591971" 
    } 
    [2]=> array(2) { 
        [0]=> float(2.0566666666667) 
        [1]=> string(8) "17015102" 
    } 
    [3]=> array(2) { 
        [0]=> float(2.0366666666667) 
        [1]=> string(7) "9637191" 
    } 
    [4]=> array(2) { 
        [0]=> float(2.015) 
        [1]=> string(8) "11405473" 
    } 
    [5]=> array(2) { 
        [0]=> float(1.9833333333333) 
        [1]=> string(8) "28233403" 
    } 
    [6]=> array(2) { 
        [0]=> float(2.0366666666667) 
        [1]=> string(8) "14248330" 
    } 
    [7]=> array(2) { 
        [0]=> float(2.0933333333333) 
        [1]=> string(8) "14987165" 
    } 
}

After I use the function arsort() it looks like this:

array(8) { 
    [0]=> array(2) { 
        [0]=> float(2.1166666666667) 
        [1]=> string(7) "9434493" 
    } 
    [7]=> array(2) { 
        [0]=> float(2.0933333333333) 
        [1]=> string(8) "14987165" 
    } 
    [1]=> array(2) { 
        [0]=> float(2.07) 
        [1]=> string(7) "8591971" 
    } 
    [2]=> array(2) { 
        [0]=> float(2.0566666666667) 
        [1]=> string(8) "17015102" 
    } 
    [6]=> array(2) { 
        [0]=> float(2.0366666666667) 
        [1]=> string(8) "14248330" 
    } 
    [3]=> array(2) { 
        [0]=> float(2.0366666666667) 
        [1]=> string(7) "9637191" 
    } 
    [4]=> array(2) { 
        [0]=> float(2.015) 
        [1]=> string(8) "11405473" 
    } 
    [5]=> array(2) { 
        [0]=> float(1.9833333333333) 
        [1]=> string(8) "28233403" 
    } 
} 

So all cool that my array is sorted by the value of [0] index. But.....

When I try to loop through like this...

$x = 0;
while ($x < count($sorted_array)) {
    $sorted_array[$x][0];
    $x++;
}

It kept printing out the original array order. I then realized when I used the function arsort() it kept the original order of the indexes so that's why it was printing in the original array order.

Is there a function to fix this so I can loop it with an index? Any help will be much appreciated.

2
  • 2
    foreach() loop Commented Feb 18, 2018 at 4:28
  • Use rsort function instead of arsort, if you don't want to keep index position with value. Commented Feb 18, 2018 at 5:48

3 Answers 3

4

When you use arsort() you preserve the keys.

Because you are iterating using $x, you are effectively ignoring your sort call.

Either use rsort() with your loop.

Or use a foreach() loop after your arsort() call.

Or best, just call array_column() instead of looping.

Here are some demonstrations: (Demo Link)

$array=$copy=[
    [2.1166666666667,9434493],
    [2.07,8591971],
    [2.0566666666667,17015102],
    [2.0366666666667,9637191],
    [2.015,11405473],
    [1.9833333333333,28233403],
    [2.0366666666667,14248330],
    [2.0933333333333,14987165]
];
arsort($array);
var_export(array_column($array,0));  // <-- you lose the keys you preserved
echo "\n---\n";

foreach($array as $index=>$row){     // <-- you keep the keys you preserved
    echo "$index : {$row[0]}\n";
}

echo "\n---\n";
rsort($copy);                        // you don't preserve the keys
for($x=0, $count=sizeof($copy); $x<$count; ++$x){  // you should cache the count instead of calling count() on every iteration
    echo "$x : {$copy[$x][0]}\n";
}

Output:

array (
  0 => 2.1166666666667,
  1 => 2.0933333333333,
  2 => 2.07,
  3 => 2.0566666666667,
  4 => 2.0366666666667,
  5 => 2.0366666666667,
  6 => 2.015,
  7 => 1.9833333333333,
)
---
0 : 2.1166666666667
7 : 2.0933333333333
1 : 2.07
2 : 2.0566666666667
6 : 2.0366666666667
3 : 2.0366666666667
4 : 2.015
5 : 1.9833333333333

---
0 : 2.1166666666667
1 : 2.0933333333333
2 : 2.07
3 : 2.0566666666667
4 : 2.0366666666667
5 : 2.0366666666667
6 : 2.015
7 : 1.9833333333333
Sign up to request clarification or add additional context in comments.

1 Comment

I will use one of these methods. I'll let you know how it goes.
3

rsort() is great fit here.

Sort an array in reverse order

This function does not maintain index association

Comments

1

A quick look at PHP documentation suggests rsort() would work.

http://php.net/manual/en/array.sorting.php

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.