Foreach 像这样遍历多维数组
Foreach loop through multidimentional array like this
我在变量 $comments
中有一个多维数组,包含:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => appr
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => appr
)
// There is more...
)
如何遍历此数组并使用 for each 回显每个值?
foreach($arrayOfArrays as $array){
foreach($array as $index => $value){
echo $array[$index];
}
}
您应该使用两个 foreach
循环,因为您的数组必须达到以下水平:
foreach ($comments as $comment)
foreach ($comment as $comment_data)
echo $comment_data;
如果您的数组结构与您显示的一样,您可以按照以下方式执行此操作:
foreach($comments as $comment) {
echo $comment[0];
echo $comment[1];
echo $comment[2];
echo $comment[3];
}
只需使用两个 foreach 循环。一个在另一个里面
foreach($comments as $commentArray){
foreach($commentArray as $comment){
echo $comment;
}
}
希望对您有所帮助
$i=0;
$c=count($array);
while ($i<$c) {
foreach ($array[$i] as $comment_property) {
echo $comment_property;
}
$i++;
}
我在变量 $comments
中有一个多维数组,包含:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => appr
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => appr
)
// There is more...
)
如何遍历此数组并使用 for each 回显每个值?
foreach($arrayOfArrays as $array){
foreach($array as $index => $value){
echo $array[$index];
}
}
您应该使用两个 foreach
循环,因为您的数组必须达到以下水平:
foreach ($comments as $comment)
foreach ($comment as $comment_data)
echo $comment_data;
如果您的数组结构与您显示的一样,您可以按照以下方式执行此操作:
foreach($comments as $comment) {
echo $comment[0];
echo $comment[1];
echo $comment[2];
echo $comment[3];
}
只需使用两个 foreach 循环。一个在另一个里面
foreach($comments as $commentArray){
foreach($commentArray as $comment){
echo $comment;
}
}
希望对您有所帮助
$i=0;
$c=count($array);
while ($i<$c) {
foreach ($array[$i] as $comment_property) {
echo $comment_property;
}
$i++;
}