[英]How can i set multidimensional foreach loop in php?
我有两个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
但是确实解决了这个问题。 谁能指导我如何转换为这种格式。 谢谢
我认为,基本上,嵌套的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++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.