PHP foreach 循环遍历多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/842956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP foreach loop through multidimensional array
提问by Greg
I have an array:
我有一个数组:
$arr_nav = array( array( "id" => "apple",
"url" => "apple.html",
"name" => "My Apple"
),
array( "id" => "orange",
"url" => "orange/oranges.html",
"name" => "View All Oranges",
),
array( "id" => "pear",
"url" => "pear.html",
"name" => "A Pear"
)
);
Which I would like to use a foreach loop to replace (which only allows me to set the number:
我想使用 foreach 循环来替换它(它只允许我设置数字:
for ($row = 0; $row < 5; $row++)
with the ability to display a .firstand .lastclass for the relevant array values
能够显示相关数组值的a.first和.last类
Edit
编辑
I would like the data to be echoed as:
我希望将数据回显为:
<li id="' . $arr_nav[$row]["id"] . '"><a href="' . $v_url_root . $arr_nav[$row]["url"] . '" title="' . $arr_nav[$row]["name"] . '">"' . $arr_nav[$row]["name"] . '</a></li>' . "\r\n";
Many thanks for your quick responses. StackOverflow rocks!
非常感谢您的快速回复。StackOverflow 太棒了!
回答by Greg
$last = count($arr_nav) - 1;
foreach ($arr_nav as $i => $row)
{
$isFirst = ($i == 0);
$isLast = ($i == $last);
echo ... $row['name'] ... $row['url'] ...;
}
回答by Hassan Amir Khan
<?php
$php_multi_array = array("lang"=>"PHP", "type"=>array("c_type"=>"MULTI", "p_type"=>"ARRAY"));
//Iterate through an array declared above
foreach($php_multi_array as $key => $value)
{
if (!is_array($value))
{
echo $key ." => ". $value ."\r\n" ;
}
else
{
echo $key ." => array( \r\n";
foreach ($value as $key2 => $value2)
{
echo "\t". $key2 ." => ". $value2 ."\r\n";
}
echo ")";
}
}
?>
OUTPUT:
输出:
lang => PHP
type => array(
c_type => MULTI
p_type => ARRAY
)
回答by soulmerge
If you mean the first and last entry of the array when talking about a.first and a.last, it goes like this:
如果您在谈论 a.first 和 a.last 时指的是数组的第一个和最后一个条目,则如下所示:
foreach ($arr_nav as $inner_array) {
echo reset($inner_array); //apple, orange, pear
echo end($inner_array); //My Apple, View All Oranges, A Pear
}
arrays in PHP have an internal pointer which you can manipulate with reset, next, end. Retrieving keys/values works with keyand current, but using eachmight be better in many cases..
PHP 中的数组有一个内部指针,您可以使用reset、next、end 对其进行操作。检索键/值适用于key和current,但在许多情况下使用每个可能会更好..
回答by PatrikAkerstrand
<?php
$first = reset($arr_nav); // Get the first element
$last = end($arr_nav); // Get the last element
// Ensure that we have a first element and that it's an array
if(is_array($first)) {
$first['class'] = 'first';
}
// Ensure we have a last element and that it differs from the first
if(is_array($last) && $last !== $first) {
$last['class'] = 'last';
}
Now you could just echo the class inside you html-generator. Would probably need some kind of check to ensure that the class is set, or provide a default empty class to the array.
现在你可以在你的 html-generator 里面回显这个类。可能需要进行某种检查以确保设置了类,或者为数组提供默认的空类。

