php - How to merge the output of two associative arrays with no unique keys? -


i've got feeling stuck seemed trivial task. please take @ $data array. data skeleton multilevel menue. $data['name'] contains names given menue points/buttons , $data['url'] corresponding links. array dimensions $data['name'] , $data['url'] in sync - exact same nodes, same element count , on.

having started write simple output function - names first. know code isn't pretty works. beat me mind aches when mentioning recursion ;) nevertheless primitive way leads proper output. in second step needed add url information corresponding names/buttons fail , begun feel dumb.

while iterating through $data['name'] need pointer current element , use corresponding url $data['url'] nope - associative array way leads dead end. keys aren't unique way ... easy sigh array shortened version of original 1 generated 150 pages long word document still growing , changing.

any ideas how output button-names + corresponding url's ? placed working code on codepad.org fiddling around.

my array:

<?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',           ),         ),       ),      )     );     

my code:

//-----------------------------------------------------------          menueoutput($data);       //-----------------------------------------------------------      function menueoutput($str)     {     foreach($str $index =>$atom)     {                                                               if(is_array($atom))         {             echo "\n\r>".$index;              foreach($atom $index2 => $atom2)             {                 if(is_array($atom2))                 {                         echo "\n\r>>".$index2;                      foreach($atom2 $index3 => $atom3)                     {                         if(is_array($atom3))                         {                                 echo "\n\r>>>".$index3;                              foreach($atom3 $index4 => $atom4)                             {                                 if(is_array($atom4))                                 {                                     echo "\n\r>>>>".$index4;                                      foreach($atom4 $index5 => $atom5)                                     {                                         if(is_array($atom5))                                         {                                             echo "\n\r>>>>>".$index5;                                             foreach($atom5 $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;         }          }     }       //-----------------------------------------------------------      

update:

i'm considering merge partial arrays parallel loops. found multipleiterator example may need (if i'll lazy). otherwise i'll forced refactor data can painful. if revent answer become useful.

what menu systems have multi-dimensional array info 1 menu item in 1 element of array. example

$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', ...)     ),     ... ); 

i go through pain of fetching data , putting array easy output screen.

without unique ids in arrays, left match them position in array, may or may not reliable. if have control on data going them highly recommend rebuilding array makes more sense. if have on 150 items need efficient can array design.

update: here how structure data:

<?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')                   )                )             )          ),          ...       )     )   ); ?> 

as can see, each menu item stores both name , url. if there children, add third element array children in similar array. php code extract data loops should simplified shown below:

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

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -