如何从数组中检索正确的值并将其包含在链接中?

时间:2019-07-16 18:13:57

标签: php loops codeigniter foreach codeigniter-3

如何处理多个foreach循环并从数组中检索值?简而言之, 我从MySQL读取了子类别中的所有数组,然后为每个类别加入该子类别插入数组中。 现在,我收到每个类别的所有子类别了!

我在这里做错了-感谢您的帮助。

Categories
Item1 = Appetizer
Item2 = Bread
Item3 = Beverage
....

Subcategories
SubItem1 -> array(Appetizer,Bread)
SubItem2 -> array(Bread)
SubItem3 -> array(Appetizer)
SubItem4 -> array(Appetizer,Bread,Beverage)
.....

My Result is:
Item1 = SubItem1 – SubItem2 - SubItem3 - SubItem4
 or
Item2 = SubItem1 – SubItem2 - SubItem3 - SubItem4
.....

Right Result:
Item1 = SubItem1 - SubItem3 - SubItem4
 or
Item2 = SubItem1 – SubItem2
.....

目前,我有:

   <?php foreach ($menu_links as $item): ?>

   <?php $valuesub = explode(",",$page->subcat_recip_id);
  if ($item['parent_id'] != "0" && $item['subcat_recip_id'] != "0"):?>

  <li><?php foreach($valuesub as $dt): echo ($dt==$item['title'])?$item["slug"]:""; ?>
    <a href="<?php echo html_escape($item["title"]); ?>"><?php echo html_escape($item["title"]) ?></a><?php endforeach;?></li>
    <?php endif; endforeach; ?>

这就是$ menu_links:

 //get menu links by language
public function get_menu_links_by_lang()
{
    $lang_id = $this->input->post('lang_id', true);
    if (!empty($lang_id)):
        $menu_links = $this->navigation_model->get_menu_links_by_lang($lang_id);
        foreach ($menu_links as $item):
            if ($item["type"] != "category" && $item["location"] == "header" && $item['parent_id'] == "0"):
                echo ' <option value="' . $item["id"] . '">' . $item["title"] . '</option>';
            endif;
        endforeach;
    endif;
}

0 个答案:

没有答案