Php 合并数组
我有一个数组,如下所示: 在这个例子中,同一个品牌id有多个产品,我想将同一品牌id合并到一个数组中,并在品牌内部有产品数组 对此有什么建议吗Php 合并数组,php,arrays,Php,Arrays,我有一个数组,如下所示: 在这个例子中,同一个品牌id有多个产品,我想将同一品牌id合并到一个数组中,并在品牌内部有产品数组 对此有什么建议吗 谢谢。创建一个新阵列,将品牌id作为阵列密钥: $res = array(); foreach ($array as $row) { if (!isset($res[$row['brand_id']])) { $res[$row['brand_id']] = array( 'brand_logo' => $row['bran
谢谢。创建一个新阵列,将品牌id作为阵列密钥:
$res = array();
foreach ($array as $row) {
if (!isset($res[$row['brand_id']])) {
$res[$row['brand_id']] = array(
'brand_logo' => $row['brand_logo'],
'brand_discovered_count' => $row['brand_discovered_count'],
'brand_name' => $row['brand_name'],
'brand_template_id' => $row['brand_template_id'],
'products' => array()
);
}
$res[$row['brand_id']]['products'][] = array(
'product_id' => $row['product_id'],
'product_price' => $row['product_price']
);
}
关于合并PHP数组,有成千上万的问题/答案。你的肯定不是第一个这样的人。试试看,你的预算是多少?如果你想让我们做所有的工作,你不能期望它是免费的,对吗?这甚至不是一个有效的PHP代码-字符串应该在引号中。还有,你听说过代码格式吗?想象一下,SO上的代码占位符不是超宽的。还有一个原因——代码行不能超过80个字符!谢谢你的回答和宝贵的时间