[英]In PHP/Opencart, how to put an array inside another 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);
}
这是代码视图:
<?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) { ?>
“>
尽管我认为这可能更适合您要实现的目标,但是您在这里的方向正确:
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']
数组中。
希望这会有所帮助。
根据讨论,只需将[]
从'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']
)
);
这可能为您工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.