在PHP/Opencart中,如何将一个数组放在另一个数组中?
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']
)
);
这可能对你有用。
我是 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']
)
);
这可能对你有用。