如何在php中输出数组?
如果我将上述数组分配给名为如何在php中输出数组?,php,arrays,Php,Arrays,如果我将上述数组分配给名为$hello的变量,现在,我只想使用循环输出菜单-162,菜单-219 如果我只想输出属性标题值,如果我只想输出href的值 如何编写这些循环 Array ( [menu-162] => Array ( [attributes] => Array ( [title] => example1 ) [href] => nod
$hello
的变量,现在,我只想使用循环输出菜单-162
,菜单-219
如果我只想输出属性标题值,如果我只想输出href的值
如何编写这些循环
Array
(
[menu-162] => Array
(
[attributes] => Array
(
[title] => example1
)
[href] => node/13
[title] => test1
)
[menu-219] => Array
(
[attributes] => Array
(
[title] => example2
)
[href] => node/30
[title] => test2
)
)
它应该输出attr和href
它应该输出attr和href 您可以访问属性数组中的标题值,如下所示:
$hello['menu-162']['attributes']['title']
对于任何其他“菜单”,您可以使用适当的菜单编号组合替换menu-162
。至于href
一个简单的$hello['menu-162']['href']
对于访问这两个值的循环,一个简单的foreach
就足够了:
foreach ($hello as $item) {
$attr = $item['attributes']['title'];
$href = $item['href'];
echo "attr is {$attr}";
echo "href is {$href}";
}
您可以访问属性数组中的标题值,如下所示:
$hello['menu-162']['attributes']['title']
,对于任何其他“菜单”,您可以使用适当的菜单编号组合替换menu-162
。至于href
一个简单的$hello['menu-162']['href']
对于访问这两个值的循环,一个简单的foreach
就足够了:
foreach ($hello as $item) {
$attr = $item['attributes']['title'];
$href = $item['href'];
echo "attr is {$attr}";
echo "href is {$href}";
}
如果不需要特定的菜单查找,请删除switch语句。如果您确实需要使用特定的ID,那么这是一个更具可伸缩性的解决方案,并且比嵌套的If更快。它也不会为不存在的变量创建通知,只会在属性title和href都存在时返回
如果不需要特定的菜单查找,请删除switch语句。如果您确实需要使用特定的ID,那么这是一个更具可伸缩性的解决方案,并且比嵌套的If更快。它也不会为不存在的变量创建通知,只会在属性title和href都存在时返回。这是非常基本的内容。你试过什么吗?你看了吗?试着在每次迭代中循环数组和
print\r
,看看您可以使用什么?请添加一个示例,说明您希望输出的样子这是非常基本的东西。你试过什么吗?你看了吗?试图在每次迭代中循环数组和print\r
,以查看您可以使用什么?请添加一个示例,说明您希望输出的样子
foreach($hello as $key => $value) {
switch($key) {
case 'menu-162':
case 'menu-219':
if($value['href'] && $value['attribute'] && $value['attribute']['title']) {
$href = $value['href'];
$attr = $value['attribute']['title'];
}
break;
default:
continue; //didn't find it
break;
}
}