我正在测试一个递归的PHP脚本。但关联数组以" items"开头。 key正在抛出错误T_DOUBLE_ARROW。我需要在这部分使用括号吗? "方法" key是" items"中的一个层。键。有人可以指导我如何解决这个错误吗?调用name,subject和type键时,find_in_arr函数可以正常工作。但当它到达项目时,我得到了错误。
<?php
function find_in_arr($key, $arr)
{
foreach ($arr as $k => $v)
{
if ($k == $key)
{
return $v;
}
if (is_array($v))
{
foreach ($v as $_k => $_v)
{
if ($_k == $key)
{
return $_v;
}
}
}
}
return false;
}
$arr =
array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => ("one" => "Iteration","two" => "Recursion",
"methods" => ("factorial" => "Recursion","fibonacci" => "Recursion"),)
"parent"? => "Larry Ullman",
);
var_dump
(
find_in_arr('two', $arr),
find_in_arr('parent', $arr),
find_in_arr('fibonacci', $arr)
//find_in_arr('name', $arr),
//find_in_arr('subject', $arr),
//find_in_arr('type', $arr)
);
答案 0 :(得分:1)
应该是:
$arr = array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => array(
"one" => "Iteration",
"two" => "Recursion",
"methods" => array(
"factorial" => "Recursion",
"fibonacci" => "Recursion"
)
),
"parent" => "Larry Ullman"
);