php - 如何合并两个没有唯一键的关联数组的输出?

标签 php arrays associative-array

我有一种感觉,我被一些看似微不足道的任务困住了。请查看 $data 数组。这是多级菜单的数据框架。 $data['name'] 包含给定菜单点/按钮的名称和 $data['url'] 相应的链接。数组维度 $data['name']$data['url'] 完全同步 - 具有完全相同的节点、相同的元素计数等。

有了这个我开始写一个简单的输出函数——首先只有名字。我知道代码不漂亮,但它可以工作。打败我,但在提到递归时我的心很痛;)尽管如此,我的原始方式导致了正确的输出。在第二步中,我需要添加与名称/按钮对应的 url 信息,但这是我失败并开始感到愚蠢的地方。

在遍历 $data['name'] 时,我需要一个指向当前元素的指针,并使用它从 $data['url'] 但不是 - 这是一个关联数组,所以这种方式会导致死胡同。顺便说一句,键不是唯一的……这很容易叹气这个数组只是原始数组的缩短版本,它是从一个 150 页长的 word 文档生成的,该文档仍在增长和改变。

关于如何输出按钮名称 + 相应 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 EXAMPLE 这可能是我需要的(如果我懒惰的话)。否则我将被迫重构我的数据,这会很痛苦。如果我这样做,Revent 答案将变得非常有用。

最佳答案

我对菜单系统所做的是拥有一个多维数组,该数组的一个元素中包含一个菜单项的所有信息。例如

$menu = array(
   'top_level' => array(
      0 => array('name'=>'Item 1','url' =>'Url 1', ...),
      1 => array('name'=>'Item 2','url' =>'Url 2', ...)
    ),
    'sub_level' => array(
      0 => array('name'=>'Item 1','url' =>'Url 1', ...),
      1 => array('name'=>'Item 2','url' =>'Url 2', ...)
    ),
    ...
);

最初我经历了获取数据并将其放入易于输出到屏幕的数组的痛苦。

如果您的数组中没有唯一 ID,您只能通过数组中的位置来匹配它们,这可能可靠也可能不可靠。如果您可以控制进入它们的数据,我强烈建议将您的阵列重建为更有意义的东西。如果您有超过 150 个项目,那么您需要尽可能高效地进行阵列设计。

更新: 以下是我可能构建数据的方式:

<?php
$data = array (
  'basics' => array ('name'=>'basics', url=>'basics', 'children'=> 
      array (
         0 => array('name'=>'about', 'url'=>'basics.about'),
         1 => array('name'=>'why_do_i_need_it', 'url'=>'basics.why_do_i_need_it'),
         2 => array('name'=>'institutions', 'url'=>'basics.institutions'),
         3 => array('name'=>'agencys', 'url'=>'basics.agencys'),
         4 => array('name'=>'impact', 'url'=>'basics.impact'),
         5 => array('name'=>'failing_this_test', 'url'=>'basics.failing_this_test'),
         6 => array('name'=>'evaluation_criteria', 'url'=>'basics.evaluation_criteria'),
         7 => array('name'=>'evaluation_range', 'url'=>'basics.evaluation_range'),
         8 => array('name'=>'reissue_request', 'url'=>'basics.reissue_request'),
         9 => array('name'=>'procedure', 'url'=>'basics.procedure', 'children' =>
            array(
               0 => array('name'=>'psych', 'url'=>'psych', 'children'=>
                  array(
                     0 => array('name'=>'test_1', 'url'=>'test_1'),
                     1 => array('name'=>'test_2', 'url'=>'test_2'),
                     2 => array('name'=>'test_3', 'url'=>'test_3'),
                     3 => array('name'=>'test_4', 'url'=>'test_4'),
                     4 => array('name'=>'test_5', 'url'=>'test_5')
                  )
               ),
               1 => array('name'=>'med', 'url'=>'med', 'children'=>
                  array(
                     0 => array('name'=>'examination', 'url'=>'examination'),
                     1 => array('name'=>'exploration', 'url'=>'exploration'),
                     2 => array('name'=>'observation', 'url'=>'observation')
                  )
               )
            )
         ),
         ...
      )
    )
  );
?>

如您所见,每个菜单项都存储名称和 url。如果有 child ,您只需将第三个元素添加到该数组, child 在一个类似的数组中。提取数据循环的 PHP 代码应该大大简化,如下所示:

foreach ( $data as $top )
{
   echo '<br><a href="'.$top['url'].'">'.$top['name'].'</a>';   // print out the menu item

   if ( array_key_exists('children', $top) )
   {
      foreach ($top['children'] as $level2)
      {
         echo '<br>&nbsp;-&nbsp;<a href="'.$level2['url'].'">'.$level2['name'].'</a>';   // print out the children
         if ( array_key_exists('children', $level2) )
         {
            foreach ($level2['children'] as $level3)
            {
               echo '<br>&nbsp;&nbsp;&nbsp;-&nbsp;<a href="'.$level3['url'].'">'.$level3['name'].'</a>';   // print out the grandchildren
               if ( array_key_exists('children', $level3) )
               {
                  // process the greatgrandchildren, etc.
               }
            }
         }
      }
   }
}

关于php - 如何合并两个没有唯一键的关联数组的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15823309/

相关文章:

php,从 mysql 获取一行并设置一个变量以显示链接

php - 使用准备好的语句和存储过程获取 InsertId?

javascript - 无法读取未定义的属性 '0'

php - 显示表上的 JavaScript 错误

PHP警告: cannot modify header information

mysql - 在mysql中插入逗号分隔的数组

php - Cakephp如何删除链接上的下划线

d - 测试关联数组是否包含 D 中的键

php - 将关联数组插入另一个数组

php - 将数据库表中的分组值与关联数组进行比较