在php中遍歷數據一般會使用到list,foreach,each其實中一種,但下面的教程可能不會用到,下面我來給各位分別介紹一下遍歷數組高級實例,希望此方法對大家有幫助.
學習程式語言時,總是學學 for,然后再試著用 while 寫出 for 的效果等等的一些練習,來看看沒有 foreach 前,要想要有 foreach 的功能要怎?寫(用 while、list、each 來達成).
在這篇文章看到: PHP的foreach前身寫法,代碼如下:
- //舊的寫法
- reset($attributes);
- while (list($key, $value) = each($attributes)) {
- //do something
- }//開源代碼Vevb.com
- //PHP4版本新增
- foreach ($attributes as $key => $value){
- //do something
- }
多維關聯數組排序
PHP提供了一些數組排序的函數,比如sort(), ksort(),和asort(),但是卻沒有提供對多維關聯數組的排序.
比如這樣的數組,代碼如下:
- Array
- (
- [0] => Array
- (
- [name] => chess
- [price] => 12.99
- )
- [1] => Array
- (
- [name] => checkers
- [price] => 9.99
- )
- [2] => Array
- (
- [name] => backgammon
- [price] => 29.99
- )
- )
要將該數組按照升序排序,你需要自己寫一個函數用于比較價格,然后將該函數作為回調函數傳遞給usort()函數來實現該功能,代碼如下:
- function comparePrice($priceA, $priceB){
- return $priceA['price'] - $priceB['price'];
- }
usort($games, 'comparePrice');執行了該程序片段,數組就會被排序,結果如下所示:
- Array
- (
- [0] => Array
- (
- [name] => checkers
- [price] => 9.99
- )
- [1] => Array
- (
- [name] => chess
- [price] => 12.99
- )
- [2] => Array
- (
- [name] => backgammon
- [price] => 29.99
- )
- )
要將該數組按照降序排序,把comparePrice()函數里面的兩個減的數調換位置就可以了.
逆序遍歷數組
PHP的While循環和For循環是遍歷一個數組最常用的方法,但是你怎樣遍歷像下面這個數組呢?代碼如下:
- Array
- (
- [0] => Array
- (
- [name] => Board
- [games] => Array
- (
- [0] => Array
- (
- [name] => chess
- [price] => 12.99
- )
- [1] => Array
- (
- [name] => checkers
- [price] => 9.99
- )
- )
- )
- )
PHP標準庫中有一個對集合的迭代器iterators類,它不僅僅能夠用于遍歷一些異構的數據結構,比如文件系統和數據庫查詢結果集,也可以對一些不知道大小的嵌套數組的遍歷,比如對上面的數組的遍歷,可以使用RecursiveArrayIterator迭代器進行,代碼如下:
- $iterator = new RecursiveArrayIterator($games);
- iterator_apply($iterator, 'navigateArray', array($iterator));
- function navigateArray($iterator) {
- while ($iterator->valid()) {
- if ($iterator->hasChildren()) {
- navigateArray($iterator->getChildren());
- } else {
- printf("%s: %s", $iterator->key(), $iterator->current());
- }
- $iterator->next();
- }
- }
- //執行該段代碼會給出以下的結果:
- name: Board
- name: chess
- price: 12.99
- name: checkers
- price: 9.99
過濾關聯數組的結果
假定你得到了如下一個數組,但是你僅僅想操作價格低于$11.99的元素,代碼如下:
- Array
- (
- [0] => Array
- (
- [name] => checkers
- [price] => 9.99
- )
- [1] => Array
- (
- [name] => chess
- [price] => 12.99
- )
- [2] => Array
- (
- [name] => backgammon
- [price] => 29.99
- )
- )
使用array_reduce()函數可以很簡單的實現,代碼如下:
- function filterGames($game){
- return ($game['price'] < 11.99);
- }
- $names = array_filter($games, 'filterGames');
array_reduce()函數會過濾掉不滿足回調函數的所有的元素,本例子的回調函數就是filterGames,任何價格低于11.99的元素會被留下,其他的會被剔除,該代碼段的執行結果,代碼如下:
- Array
- (
- [1] => Array
- (
- [name] => checkers
- [price] => 9.99
- )
- )
對象轉換成數組
一個需求就是將對象轉換成數組形式,方法比你想象的簡單很多,僅僅強制轉換就可以了,例子代碼如下:
- class Game {
- public $name;
- public $price;
- }
- $game = new Game();
- $game->name = 'chess';
- $game->price = 12.99;
print_r(array($game));執行該例子就會產生如下結果:
- Array
- (
- [0] => Game Object
- (
- [name] => chess
- [price] => 12.99
- )
- )
將對象轉換成數組會出現一些不可預料的副作用,比如上面的代碼段,所有的成員變量都是public類型的,但是對于private私有變量的返回結果會變得不一樣,下面是另外一個例子,代碼如下:
- class Game {
- public $name;
- private $_price;
- public function setPrice($price) {
- $this->_price = $price;
- }
- }
- $game = new Game();
- $game->name = 'chess';
- $game->setPrice(12.99);
- print_r(array($game));
- //執行該代碼片段:
- Array
- (
- [0] => Game Object
- (
- [name] => chess
- [_price:Game:private] => 12.99
- )
- )
正如你所看到的,為了進行區分,數組中保存的私有變量的key被自動改變了.
數組的“自然排序”
PHP對于“字母數字”字符串的排序結果是不確定的,舉個例子,假定你有很多圖片名稱存放于數組中,代碼如下:
- $arr = array(
- 0=>'madden2011.png',
- 1=>'madden2011-1.png',
- 2=>'madden2011-2.png',
- 3=>'madden2012.png'
- );
你怎樣對這個數組進行排序呢?如果你用sort()對該數組排序,結果是這樣的,代碼如下 :
- Array
- (
- [0] => madden2011-1.png
- [1] => madden2011-2.png
- [2] => madden2011.png
- [3] => madden2012.png
- )
有時候這就是我們想要的,但是我們想保留原來的下標怎么辦?解決該問題可以使用natsort()函數,該函數用一種自然的方法對數組排序,代碼如下:
- <?php
- $arr = array(
- 0=>'madden2011.png',
- 1=>'madden2011-1.png',
- 2=>'madden2011-2.png',
- 3=>'madden2012.png'
- );
- natsort($arr);
- echo "<pre>"; print_r($arr); echo "</pre>";
- ?>
- //運行結果:
- Array
- (
- [1] => madden2011-1.png
- [2] => madden2011-2.png
- [0] => madden2011.png
- [3] => madden2012.png
- )
新聞熱點
疑難解答