6
array(7) {
  [0]=> array(2) { ["id"]=> string(1) "9"  ["roi"]=> float(0)    }
  [1]=> array(2) { ["id"]=> string(1) "1"  ["roi"]=> float(0)    }
  [2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0)    }
  [3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0)    }
  [4]=> array(2) { ["id"]=> string(1) "4"  ["roi"]=> float(0)    }
  [5]=> array(2) { ["id"]=> string(1) "5"  ["roi"]=> float(141)  }
  [6]=> array(2) { ["id"]=> string(1) "6"  ["roi"]=> float(2600) }
}

I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.

How can I do this? array_reverse() and rsort() does not work in this case

2
  • 1
    none of these are the correct answer? Commented Mar 5, 2016 at 13:10
  • 1
    Just going to leave this one hanging? If none helped you then post your solution and mark it accepted. Commented Jul 6, 2016 at 14:12

6 Answers 6

24

http://php.net/manual/en/function.array-reverse.php:

$newArray = array_reverse($theArray, true);

The important part is the true parameter, which preserves the keys.

Not convinced? You can see it in action on this codepad exampole.

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

3 Comments

Because they want to keep the key associativity?
@Kolink so??? array_reverse() does that. I suggest you read the docs.
Ah, so it does, with the extra preserve_keys parameter. My mistake.
1
foreach($array as $arr){

  array_unshift($array, $arr); 
  array_pop($array);

}

Comments

1
$res = array(
    0=>array("id"=>9, "roi"=>0),
    1=>array("id"=>1,"roi"=>0),
    2=>array("id"=>10,"roi"=>0),
    3=>array("id"=>14,"roi"=>0),
    4=>array("id"=>4,"roi"=>0),
    5=>array("id"=>5,"roi"=>141),
    6=>array("id"=>6,"roi"=>2600));

$res4   =   array(); 
$count  = count($res);

for($i=$count-1;$i>=0;$i--){
    $res4[$i] =$res[$i]; 
}

print_r($res4);

Comments

0

You can use an usort() function, like so

$arr = array('......'); // your array
usort($arr, "my_reverse_array");

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

This will make sure the item with the highest roi is first in the array.

Comments

0
$res = array(
    0=>array("id"=>9, "roi"=>0),
    1=>array("id"=>1,"roi"=>0),
    2=>array("id"=>10,"roi"=>0),
    3=>array("id"=>14,"roi"=>0),
    4=>array("id"=>4,"roi"=>0),
    5=>array("id"=>5,"roi"=>141),
    6=>array("id"=>6,"roi"=>2600));
    $count = count($res);

    for ($i=0, $j=$count-1; $i<=floor($count/2); $i++, $j--) {
        $temp = $res[$j];
        $res[$j] = $res[$i];
        $res[$i] = $temp;
    }
    echo '<pre>';
    print_r($res);
    echo '</pre>';

Comments

0

It's easy. May be use usort function of php like:

usort($arr, function($a, $b) {    
  return $b['roi'] - $a['roi'];    
});

Just swap position $a and $b that is correct.

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.