些細な仕事に見えた何かに行き詰まっているような気がします。$data 配列を見てください。これは、マルチレベル メニューのデータ スケルトンです。$data['name']には、指定されたメニュー ポイント/ボタンの名前と、$data['url']対応するリンクが含まれます。配列の次元$data['name']と$data['url']は完全に同期しています - まったく同じノード、同じ要素数などです。
これで、最初に名前だけを付けて、簡単な出力関数を書き始めました。コードがきれいではないことはわかっていますが、機能します。私を打ちのめしますが、再帰について言及すると心が痛みます;) それでも、私の原始的な方法は適切な出力につながります。2 番目のステップでは、名前/ボタンに対応する URL 情報を追加する必要がありましたが、ここで失敗し、頭がおかしくなり始めました。
$data['name']を繰り返し処理している間、現在の要素へのポインターが必要であり、それを使用して$data['url']から対応する URL を取得しますが、いいえ - これは連想配列であるため、この方法ではデッドにつながります終わり。ちなみに、キーは一意ではありません...これは簡単にため息をつきます.この配列は、元の配列の短縮バージョンにすぎません。これは、150ページの長い単語文書から生成されたもので、まだ成長し変化しています.
ボタン名 + 対応する URL を出力する方法はありますか? いじるために、 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 の回答を行うと、非常に便利になります。