简体   繁体   English

如何遍历三维数组中的数组之一

[英]How to loop through one of arrays inside three dimensional array

I have an array $data, that contains around 50 values, one of them is 'categories'. 我有一个数组$ data,包含大约50个值,其中一个是“类别”。 What i am trying to do is add another array 'subcategories'to the array 'categories', that contains categories that belong to each category and on the html side, print a list of all categories and subcategories under each corresponding category. 我想做的是在数组“类别”中添加另一个数组“子类别”,其中包含属于每个类别的类别,并且在html一侧,在每个相应类别下打印所有类别和子类别的列表。 A bit stuck here. 有点卡在这里。

initialization of arrays: 数组初始化:

$data['categories'] = array();

    $results = $this->model_catalog_category->getCategories($category_id);

    foreach ($results as $result) {


        $filter_data = array(
            'filter_category_id'  => $result['category_id'],
            'filter_sub_category' => true
        );

        $data['categories'][] = array(
            'root_categories' => array (
                'category_id' => $result['category_id'],
                'name'  => $result['name'],
                'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url),
            ),
            'subcategories' => array(
                'sub_name' => $result['name'],
                'sub_href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url),
                ),
        );


    }
    foreach ($data['categories'] as $category) {
        $sub_results = $this->model_catalog_category->getCategories($category['root_categories']['category_id']);
        foreach ($sub_results as $sub_result){
            $category['subcategories'] = array (
                'sub_name'  => $sub_result['name'],
                'sub_href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $sub_result['category_id'] . $url),
            );
        }
    }

Accesing arrays: 访问数组:

<div class="row">

  <?php if ($categories) { ?>
  <div class="row" style='margin-top: 13px; '>
    <div class="col-sm-3">
      <ul>

        <?php  foreach ($categories as $category) {  ?>
            <div class 'tests'>
            <li class='sidebar_form_item click_here_for_demo' style='padding: 5px; margin-bottom: 0px;'>
            <i class="fa fa-chevron-right" style='font-size: 12px; margin: 6px 7px 0px 7px; color: #696969; float: left;'></i>

            <div style='width: 250px; float: left;'><a href="<?php echo $category['root_categories']['href']; ?>" style='color: #696969; font-size: 12px;'><?php echo $category['root_categories']['name']; ?></a></div>
            <div class='clearfix'></div></li>

            <?php if($category['subcategories']) { ?>
                <?php foreach ($category['subcategories'] as $sub_category) { ?>

                    <li class='sidebar_form_item show_when_neded' style='display: none; padding: 5px 5px 5px 20px; margin-bottom: 0px;'>
                    <i class="fa fa-chevron-right" style='font-size: 12px; margin: 6px 7px 0px 7px; color: #696969; float: left;'></i>

                    <div style='width: 250px; float: left;'>
                    <a href="<?php echo $category[$sub_category]['sub_href']; ?>" style='color: #696969; font-size: 12px;'><?php echo $category[$sub_category]['sub_name']; ?></a></div>
                    <div class='clearfix'></div></li>

                <?php } ?>
            <?php } ?>
            </div>
        <?php } ?>


      </ul>
    </div>
  </div>
  <?php }?>

PS: Opencart website, using Model-View-Controller PS:Opencart网站,使用模型-视图-控制器

Short Answer: You are missing & before $category 简短的答案:您缺少$ category之前

Your code will not make subcategories under $data['categories'] , instead it will make a new array called $category['subcategories'] 您的代码不会在$data['categories']下创建子$data['categories'] ,而是会创建一个名为$category['subcategories']的新数组。

You can use with following code: 您可以使用以下代码:

 foreach ($data['categories'] as &$category) {
        $sub_results = $this->model_catalog_category->getCategories($category['root_categories']['category_id']);
        foreach ($sub_results as $sub_result){
            $category['subcategories'] = array (
                'sub_name'  => $sub_result['name'],
                'sub_href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $sub_result['category_id'] . $url),
            );
        }
    }

This will not create new array, rather it will take reference of $data['categories'] and will add subcategories in it, so you can access it in template file. 这不会创建新数组,而是会引用$data['categories']并在其中添加子类别,因此您可以在模板文件中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM