[英]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: 我是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);
}
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. 我们在这里所做的是首先定义
$data['products']
数组-这只会使代码更易于阅读和快速浏览。
Then, we've focused on creating a $product
array before worrying about categories; 然后,在关注类别之前,我们专注于创建
$product
数组; that way we can create our product_categories
key within the array, with a blank array as the initial value. 这样,我们可以在数组中创建
product_categories
键,并以空白数组作为初始值。
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. 接下来,在使用
getProductCategories
提取类别之后,我们枚举返回的数组,并在此过程中创建一个新的$category
数组,然后使用array_push
将其添加到$product['product_categories']
数组中。
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. 最后,对于循环,在我们完全构建了新的
$product
数组之后,再次使用array_push
将其添加到$data['products']
数组中。
Hopefully this helps. 希望这会有所帮助。
AS per the discussion, Just remove []
from 'product_categories' => $data['product_categories'][]
. 根据讨论,只需将
[]
从'product_categories' => $data['product_categories'][]
删除。 It should be 'product_categories' => $data['product_categories']
它应该是
'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. 这可能为您工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.