我有一种感觉,我被一些看似微不足道的任务所困。请看一下 $data 数组。这是多级菜单的数据框架。$data['name']包含给定菜单点/按钮的名称和$data['url']相应的链接。数组维度$data['name']和$data['url']完全同步 - 具有完全相同的节点、相同的元素计数等等。
有了这个,我开始编写一个简单的输出函数——只有名字。我知道代码并不漂亮,但它可以工作。打败我,但提到递归时我的心痛;)尽管如此,我的原始方式导致正确的输出。在第二步中,我需要添加与名称/按钮相对应的 url 信息,但这是我失败并开始感到愚蠢的地方。
在遍历$data['name']时,我需要一个指向当前元素的指针,并使用它从$data['url']获取相应的 url但不 - 这是一个关联数组,所以这种方式会导致死亡结尾。顺便说一句,键不是唯一的……这很容易叹息这个数组只是原始数组的缩短版本,它是从一个仍在增长和变化的 150 页长的 word 文档生成的。
任何想法如何输出按钮名称+相应的网址?我将工作代码放在codepad.org上,以便摆弄。
我的数组:
<?PHP
$data = array (
'name' =>
array (
'basics' =>
array (
0 => 'about',
1 => 'why_do_i_need_it',
2 => 'institutions',
3 => 'agencys',
4 => 'impact',
5 => 'failing_this_test',
6 => 'evaluation_criteria',
7 => 'evaluation_range',
8 => 'reissue_request',
9 => 'procedure',
'procedure' =>
array (
0 => 'psych',
'psych' =>
array (
0 => 'test_1',
1 => 'test_2',
2 => 'test_3',
3 => 'test_4',
4 => 'test_5',
),
'med' =>
array (
0 => 'examination',
1 => 'exploration',
2 => 'observation',
),
),
10 => 'report',
11 => 'revision',
12 => 'evaluation',
13 => 'report_structure',
14 => 'charges',
15 => 'sample_test',
),
'drugs' =>
array (
0 => 'introduction',
1 => 'requirements',
'requirements' =>
array (
0 => 'sincerity',
1 => 'excuses',
2 => 'insight',
3 => 'change',
4 => 'stability',
),
2 => 'procedure',
3 => 'diagnostics',
'diagnostics' =>
array (
0 => 'addiction',
1 => 'advanced_level',
2 => 'dangers',
3 => 'sample_drugtest',
),
4 => 'the_day_one',
5 => 'background',
6 => 'today',
7 => 'intake',
8 => 'screenings',
'screenings' =>
array (
0 => 'analysys',
1 => 'element',
),
),
'url' =>
array (
'basics' =>
array (
0 => 'basics.about',
1 => 'basics.why_do_i_need_it',
2 => 'basics.institutions',
3 => 'basics.agencys',
4 => 'basics.impact',
5 => 'basics.failing_this_test',
6 => 'basics.evaluation_criteria',
7 => 'basics.evaluation_range',
8 => 'basics.reissue_request',
9 => 'basics.procedure',
'procedure' =>
array (
0 => 'basics.procedure.psych',
'psych' =>
array (
0 => 'basics.procedure.psych.test_1',
1 => 'basics.procedure.psych.test_2',
2 => 'basics.procedure.psych.test_3',
3 => 'basics.procedure.psych.test_4',
4 => 'basics.procedure.psych.test_5',
),
'med' =>
array (
0 => 'basics.procedure.med.examination',
1 => 'basics.procedure.med.exploration',
2 => 'basics.procedure.med.observation',
),
),
10 => 'basics.report',
11 => 'basics.revision',
12 => 'basics.evaluation',
13 => 'basics.report_structure',
14 => 'basics.charges',
15 => 'basics.sample_test',
),
'drugs' =>
array (
0 => 'drugs.introduction',
1 => 'drugs.requirements',
'requirements' =>
array (
0 => 'drugs.requirements.sincerity',
1 => 'drugs.requirements.excuses',
2 => 'drugs.requirements.insight',
3 => 'drugs.requirements.change',
4 => 'drugs.requirements.stability',
),
2 => 'drugs.procedure',
3 => 'drugs.diagnostics',
'diagnostics' =>
array (
0 => 'drugs.diagnostics.addiction',
1 => 'drugs.diagnostics.advanced_level',
2 => 'drugs.diagnostics.dangers',
3 => 'drugs.diagnostics.sample_drugtest',
),
4 => 'drugs.the_day_one',
5 => 'drugs.background',
6 => 'drugs.today',
7 => 'drugs.intake',
8 => 'drugs.screenings',
'screenings' =>
array (
0 => 'drugs.screenings.analysys',
1 => 'drugs.screenings.element',
),
),
),
)
);
我的代码:
//-----------------------------------------------------------
menueOutput($data);
//-----------------------------------------------------------
function menueOutput($str)
{
foreach($str AS $index =>$atom)
{
if(is_array($atom))
{
echo "\n\r>".$index;
foreach($atom AS $index2 => $atom2)
{
if(is_array($atom2))
{
echo "\n\r>>".$index2;
foreach($atom2 AS $index3 => $atom3)
{
if(is_array($atom3))
{
echo "\n\r>>>".$index3;
foreach($atom3 AS $index4 => $atom4)
{
if(is_array($atom4))
{
echo "\n\r>>>>".$index4;
foreach($atom4 AS $index5 => $atom5)
{
if(is_array($atom5))
{
echo "\n\r>>>>>".$index5;
foreach($atom5 AS $index6 => $atom6)
{
echo "\n\r------".$atom6;
}
}
else
echo "\n\r-----".$atom5;
}
}
else
echo "\n\r----".$atom4;
}
}
else
{
echo "\n\r---".$atom3;
}
}
}
else
{
echo "\n\r--".$atom2;
}
}
}
else
{
echo "\n\r".$atom;
}
}
}
//-----------------------------------------------------------
更新:
我也在考虑通过并行循环合并部分数组。我发现这个MULTIPLEITERATOR 示例可能是我需要的(如果我懒惰的话)。否则我将被迫重构我的数据,这可能会很痛苦。如果我这样做,Revent 答案将变得非常有用。