1

I have an array with some items. Each array could have (or not) an subarray, also with some items.

How can I call the subarray in a loop? It is difficult to describe, here is the code. I know the code/syntax is not correct, but the syntax should clarify my problem:

<?php
$subitemsA = array(
    'subA1' => array('num'=>65, 'text'=>'Labor', 'url'=>'#'),
    'subA2' => array('num'=>44, 'text'=>'Rare', 'url'=>'#'),
);

$subitemsB = array(
    'subB1'   => array('num'=>0, 'text'=>'subB1', 'url'=>'#'),
    'subB2'   => array('num'=>0, 'text'=>'subB2', 'url'=>'#'),
    'subB3'   => array('num'=>0, 'text'=>'subB3', 'url'=>'#')
);

$navArray = array(
    'Home'   => array('num'=>0, 'text'=>'Home',  'url'=>'#'),
    'Info'   => array('num'=>0, 'text'=>'Info',  'url'=>'#', 'subArray'=>$subitemsA),
    'Sport'  => array('num'=>0, 'text'=>'Sport', 'url'=>'#', 'subArray'=>$subitemsB),
);


$html = '';
foreach($navArray as $item) {
    $html .= "<li>";
    $html .= "<a href='{$item['url']}'><i class='abc'></i>{$item['text']}</a>\n";

    if (count($navArray) > 3) {

        foreach($navArray.subArray as $subitem) {
            $html .= "<li>";
            $html .= "<a href='{$subitem['url']}'>{$subitem['text']}</a>\n";
            $html .= "</li>";
        }

    }

    $html .= "</li>";
}

The first foreach loop works. But how can I access the subArray of Info and Sport?

0

2 Answers 2

1

You need a three level foreach for this to work -

foreach($navArray as $key => $item) {
  $html .= "<li>";
  $html .= "<a href='{$item['url']}'><i class='abc'></i>{$item['text']}</a>\n";
  foreach ($item as $itemkey => $value) {
    if (is_array($value)) { //Now Check if $value is an array
      foreach($value as $valuekey => $subitem) { //Loop through $value
        $html .= "<li>";
        $html .= "<a href='{$subitem['url']}'>{$subitem['text']}</a>\n";
        $html .= "</li>";
      }
    }
  }
  $html .= "</li>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is an answer to your question in more general way: How to deal with multi-level nested array using recursion and template.

function parseArray(array $navArray, &$html, $depth = 0) {
  foreach ($navArray as $item) {
    $html .= "<li>";

    // this function use template to create html
    $html .= toHtml($item['url'], $item['text'], $depth);

    foreach ($item as $subItem) {
      if (is_array($subItem)) {
        // use recursion to parse deeper level of subarray
        parseArray($item, $html, $depth + 1);
      }
    }

    $html .= "</li>";

  }
}

function toHtml($url, $text, $depth)
{
  $template = '';
  if ($depth == 0) {
    $template = '<a href=\'{{url}}\'><i class=\'abc\'></i>{{text}}</a>\n';
  } elseif ($depth >= 1) {
    $template = '<a href=\'{{url}}\'>{{text}}</a>\n';
  }
  // define more template for deeper level here if you want

  $template = str_replace('{{url}}', $url, $template);
  $template = str_replace('{{text}}', $text, $template);
  return $template;
}

$html = '';
parseArray($navArray, $html);

Just hurrily forge this code out of mind, haven't test it yet. Hope it help.

Regards,

Comments

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.