在PHP / Opencart中,如何将数组放在另一个数组中?

时间:2016-05-18 06:47:11

标签: php arrays multidimensional-array opencart

我是PHP和Opencart的新手,我想用适当的类别显示产品列表,但我不知道如何管理它阵列:

$data['heading_title'] = $this->language->get('heading_title');

$results = $this->model_catalog_profile->getProducts();

if ($results) {
    foreach ($results as $result) {

        // Categories
        $categories = $this->model_profile_profile->getProductCategories($result['product_id']);

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

        foreach ($categories as $category_id) {
            $category_info = $this->model_profile_category->getCategory($category_id);
            if ($category_info) {
                $data['product_categories'][] = array(
                    'category_id' => $category_info['category_id'],
                    'name' => $category_info['name']
                );
            }
        }


        $data['products'][] = array(
            'product_id'  => $result['product_id'],
            'thumb'       => $image,
            'name'        => $result['name'],
            'product_categories'    => $data['product_categories'][]
            )
        );

    }

    return $this->load->view('module/latest', $data);
}

以下是代码视图:

<?php foreach ($products as $product) { ?>
<div class="profile-thumb transition">
    <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" /></a></div>
    <div class="caption">
        <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>
        <p><?php echo $product['description']; ?></p>

        <div id="product-category" class="" style="height: 120px; overflow: auto;">
            <?php foreach ($product_categories as $product_category) { ?>
                “&GT;                                                                  

2 个答案:

答案 0 :(得分:1)

你在这里走的正确,虽然我认为这可能更适合你想要实现的目标:

if ($results) {

  // For best practice, let's define the products key value first
  $data['products'] = array();

  foreach ($results as $result)
  {
    // Create the array for our product
    $product = array(
      'product_id'         => $result['product_id'],
      'thumb'              => $image,
      'name'               => $result['name'],
      'product_categories' => array()
    );

    // Fetch the categories for this product
    $categories = $this->model_profile_profile->getProductCategories($result['product_id']);

    foreach ($categories as $category_id)
    {
        $category_info = $this->model_profile_category->getCategory($category_id);
        if (!$category_info) continue;

        // Assign our category information a new category array
        $category = [
          'category_id' => $category_info['category_id'],
          'name'        => $category_info['name']
        ];

        // Push the new category array to our products array
        array_push($product['product_categories'], $category);

        // Optionally, free up some memory
        unset($category_info);
    }

    // Now push our new product array to our data array
    array_push($data['products'], $product);

    // Optionally, we can perform some clean up
    unset($categories);
}

return $this->load->view('module/latest', $data);

我们在此处所做的是首先定义我们的$data['products']数组 - 这只会使代码更容易阅读并快速扫描。

然后,在担心类别之前,我们专注于创建$product数组;这样我们就可以在数组中创建product_categories键,并以空白数组作为初始值。

接下来,在我们使用getProductCategories获取类别后,我们将通过返回的数组进行枚举,在此过程中创建一个新的$category数组,然后使用array_push将其添加到我们的$product['product_categories']数组。

最后,对于循环,在我们完全构建了新的$product数组之后,我们再次使用array_push将其添加到我们的$data['products']数组中。

希望这有帮助。

答案 1 :(得分:1)

根据讨论情况,只需从[]移除'product_categories' => $data['product_categories'][]即可。它应该是'product_categories' => $data['product_categories']

更新部分:

$data['products'][] = array(
    'product_id'  => $result['product_id'],
    'thumb'       => $image,
    'name'        => $result['name'],
    'product_categories'    => $data['product_categories']
    )
);

这可能适合你。