我有两个foreach循环,它的工作正常。但我需要不同格式的数据。请查看以下代码。
代码
$data['categories'] = array();
foreach ($allCategories as $result) {
$data['categories'][] = array(
'name' => $result['name'],
'Cat_id' => $result['category_id']
);
$productData= $this->model_catalog_product->getProducts($productID);
foreach ($productData as $singleProduct) {
$data['categories'][] = array(
'product_id' => $singleProduct['product_id'],
'thumb' => $image,
'name' => $singleProduct['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($singleProduct['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $singleProduct['minimum'] > 0 ? $singleProduct['minimum'] : 1,
'rating' => $rating,
'href' => $this->url->link('product/product', 'product_id=' . $singleProduct['product_id'])
);
}
}
Array
(
[0] => Array
(
[name] => sweflora special
[Cat_id] => 59
)
[1] => Array
(
[product_id] => 52
[thumb] => http://localhost/swefloraProject/upload/image/cache/catalog/birthday/home-product-01-80x80.png
[name] => gift flowers
[description] => when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has ..
[price] => $0.00
[special] =>
[tax] => $0.00
[minimum] => 1
[rating] => 0
[href] => http://localhost/swefloraProject/upload/index.php?route=product/product&product_id=52
)
[2] => Array
(
[name] => birthday
[Cat_id] => 61
)
[3] => Array
(
[product_id] => 53
[thumb] => http://localhost/swefloraProject/upload/image/cache/catalog/birthday/flower2-80x80.jpg
[name] => test flower
[description] => ..
[price] => $86.00
[special] =>
[tax] => $86.00
[minimum] => 1
[rating] => 0
[href] => http://localhost/swefloraProject/upload/index.php?route=product/product&product_id=53
)
)
我想更改数组格式。目前它直接显示类别和产品。但我需要一个像这种格式的数组。
Array
(
[0] => Array
(
[name] => sweflora special
[Cat_id] => 59
[Products] => Array
(
[product_id] => 52
[thumb] => http://localhost/Project/upload/image/cache/catalog/birthday/home-product-01-80x80.png
[name] => gift flowers
[description] => when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has ..
[price] => $0.00
[special] =>
[tax] => $0.00
[minimum] => 1
[rating] => 0
[href] => http://localhost/Project/upload/index.php?route=product/product&product_id=52
)
[Products] => Array
(
[product_id] => 52
[thumb] => http://localhost/swefloraProject/upload/image/cache/catalog/birthday/home-product-01-80x80.png
[name] => gift flowers
[description] => when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has ..
[price] => $0.00
[special] =>
[tax] => $0.00
[minimum] => 1
[rating] => 0
[href] => http://localhost/Project/upload/index.php?route=product/product&product_id=52
)
)
[1] => Array
(
[name] => sweflora special
[Cat_id] => 60
[Products] => Array
(
[product_id] => 52
[thumb] => http://localhost/Project/upload/image/cache/catalog/birthday/home-product-01-80x80.png
[name] => gift flowers
[description] => when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has ..
[price] => $0.00
[special] =>
[tax] => $0.00
[minimum] => 1
[rating] => 0
[href] => http://localhost/Project/upload/index.php?route=product/product&product_id=52
)
[Products] => Array
(
[product_id] => 52
[thumb] => http://localhost/Project/upload/image/cache/catalog/birthday/home-product-01-80x80.png
[name] => gift flowers
[description] => when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has ..
[price] => $0.00
[special] =>
[tax] => $0.00
[minimum] => 1
[rating] => 0
[href] => http://localhost/Project/upload/index.php?route=product/product&product_id=52
)
)
)
我使用了array_push
和array_combine
,但确实解决了这个问题。任何人都可以指导我如何转换为这种格式。感谢
答案 0 :(得分:1)
基本上,我认为嵌套的foreach
应该为每个类别的新内部数组添加元素。您无法完全您发布的结构,您可以将关键“产品”与多个对象相关联,但您可以构建嵌套的产品数组。所以我会尝试这个:
foreach ($productData as $singleProduct) {$data['categories']['Products'][] = array(/* Your element */);
编辑:我们需要保持与类别的约束,所以我会这样做:
$i=0;
foreach ($allCategories as $result) {
$data['categories'][$i] = array(
'name' => "N",
'Cat_id' => "C",
'Products' => array()
);
$productData= $this->model_catalog_product->getProducts($productID);
foreach ($productData as $singleProduct){
$data['categories'][$i]['Products'][] = array(
/* Your product */);
}
$i++;
}