简体   繁体   中英

In PHP/Opencart, how to put an array inside another array?

I'm newbie in PHP and Opencart and I want to show a list of products with their appropriate categories , but I don't know how to manage it with array:

$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);
}

And here is the code view:

<?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) { ?>
">

You're on the right track here, although I think this might be more appropriate to what you're trying to achieve:

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);

What we've done here is define our $data['products'] array first - this just makes the code much easier to read and quickly scan through.

Then, we've focused on creating a $product array before worrying about categories; that way we can create our product_categories key within the array, with a blank array as the initial value.

Next, after we've fetched the categories using getProductCategories , we enumerate through the returned array, creating a new $category array along the way, and then using array_push to add it to our $product['product_categories'] array.

Lastly for the loop, after we've fully constructed our new $product array, we use array_push again to add it to our $data['products'] array.

Hopefully this helps.

AS per the discussion, Just remove [] from 'product_categories' => $data['product_categories'][] . It should be 'product_categories' => $data['product_categories']

Updated Portion:

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

This may be worked for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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