php遍歷陣列的方法分享

2022-09-29 16:15:17 字數 1172 閱讀 1122

在php中陣列分為兩類www.cppcns.com: 數字索引陣列和關聯陣列。

其中數字索引陣列和c語言中的陣列一樣,下標是為0,1,2…

而關聯陣列下標可能是任意型別,與其它語言中的hash,map等結構相似。

方法1:foreach

複製** **如下:

<?php

$sports = array(

'football' => 'good',

'swimming' => 'very well',

'running' => 'not good');

foreach ($sports as $key => $value)

?>www.cppcns.com;

輸出結果:

football: good

swimming: very well

running: not good

方法2:each

複製** **如下:

<?php

$sports = array(

'football' => 'good',

'swimming' => 'v程式設計客棧ery well',

'running' => 'not good');

while (!!$elem = each($sports))

?>

輸出結果:

football: good

swimming: very well

running: not good

方法3:list & each

複製** **如下:

<?php

$sports = array(

'football' => 'good',

'swimming' => 'very well',

'running' => 'not good');

while (!!list($key, $value) = each($sports))

?>

輸出結果:

football: good

swimmingyqxlnuztb: very well

running: not good

本文標題: php遍歷陣列的方法分享

本文位址:

PHP 遍歷陣列的方法彙總

foreach 是乙個用來遍歷陣列中資料的最簡單有效的方法。example1 colors array red blue green yellow foreach colorsas color 顯示結果 do you like red?do you like blue?do you like gre...

PHP 遍歷陣列的方法彙總

from foreach 是乙個用來遍歷陣列中資料的最簡單有效的方法。example1 colors array red blue green yellow foreach colorsas color 顯示結果 do you like red?do you like blue?do you lik...

PHP 遍歷陣列的方法彙總

foreach 是乙個用來遍歷陣列中資料的最簡單有效的方法。example1 colors array red blue green yellow foreach colors as color 顯示結果 do you like red?do you like blue?do you like gr...