Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

Reverse array in php

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...