0

Consider this nested array:

$link = array(
 'Level 1' => array(
   'Monthly' => array( 
    'note' => 'Note 1 1',
    'link'  => '1.1.com',
    ),
   '6 Month' => array(  
     'note' => 'Note 1 6',
     'link' => '1.6.com',
    ),
 ),
 'Level 2' => array(
   'Monthly' => array( 
    'note' => 'Note 2.1',
    'link'  => '2.1.com',
    ),
   '6 Month' => array(  
     'note' => 'Note 2.6',
     'link' => '2.6.com',
    ),
 ),

How would I gracefully use a foreach to achieve the following:

if $var = 'Level 1' output 

  <a href="1.1.com" title="Note 1 1">Monthly</a>
  <a href="1.6.com" title="Note 1 6">6 Month</a>

I'm suspecting I might need to do a loop inside a loop? I can iterate through the array, but am having trouble figuring out how to call the name of the sub-array...

2
  • To think, I put the vars in arrays thinking it would be easier... bah Commented Nov 8, 2013 at 2:52
  • I'm thinking I should refactor the 'Monthly' into the same level as the note and link vars... in a numeric array... Commented Nov 8, 2013 at 2:56

3 Answers 3

1
<?php
$key = 'Level 1';

$link = array(
 'Level 1' => array(
   'Monthly' => array( 
    'note' => 'Note 1 1',
    'link'  => '1.1.com',
    ),
   '6 Month' => array(  
     'note' => 'Note 1 6',
     'link' => '1.6.com',
    ),
 ),
 'Level 2' => array(
   'Monthly' => array( 
    'note' => 'Note 2.1',
    'link'  => '2.1.com',
    ),
   '6 Month' => array(  
     'note' => 'Note 2.6',
     'link' => '2.6.com',
    ),
 ),
);

if(isset($link[$key])) {
    foreach($link[$key] as $array) {
        print_r($array);
    }
}
?>

RETURNS

Array
(
    [note] => Note 1 1
    [link] => 1.1.com
)
Array
(
    [note] => Note 1 6
    [link] => 1.6.com
)

I check to see if it is set first and then run a foreach on the set key to print out what you need.

EDIT:

if(isset($link[$key])) {
    foreach($link[$key] as $key => $array) {
        print $key;
        print_r($array);
    }
}

Which returns

Monthly
Array
(
    [note] => Note 1 1
    [link] => 1.1.com
)
6 Month
Array
(
    [note] => Note 1 6
    [link] => 1.6.com
)
4
  • Yes! This is the step ($link[$key]) I was f-ing ripping my hair out over... so simple once I see it. This is why I love SO Commented Nov 8, 2013 at 2:37
  • great. you got @OliverSamson43's intention. Commented Nov 8, 2013 at 2:43
  • But wait... how do I ouput the name of the arrays? (Monthly and 6 Month...) Commented Nov 8, 2013 at 2:44
  • I know, right? @fluke -- I'm so messed up in code right now, I don't know which way is up... Commented Nov 8, 2013 at 2:45
0

I think using nested loop in this case is graceful.

Just use two foreach.

2
  • So, when I'm in the second nested loop, how do I get the parent of that loop? Commented Nov 8, 2013 at 2:36
  • in every foreach loop you get a $key and a $value. $key and $value in the parent loop are from the parent. inside the parent loop, you can do a foreach to the value to get sub data. Commented Nov 8, 2013 at 2:42
0

that's how it'd look like if you're trying to define the arrays within the main array

    foreach ($link  as $sub_array) {
        //do something
        foreach ($sub_array as $sub_of_sub) {
            // do something and so on
        }
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.