根据值创建多维数组
Create multidimensional array based on value
我花了几个小时试图找到这个问题的答案,但我很挣扎。我相当熟悉 PHP 和各种内置函数,并且可以构建一个复杂的 foreach() 循环来执行此操作,但我想我会问问是否有人对我的问题有更聪明的解决方案.
我有一个这种形式的数组,现在我想根据 list_id 匹配下一个 instance.I 将其转换为多维尝试了这个解决方案但效果不佳 。
这可能是重复的,但这里没有输出
<pre>Array
(
[0] => Array
(
[list_id] => 5
[order_list_name] => meri list 3
[0] => Array
(
[list_id] => 5
[product_id] => 1
[product_name] => DUCHESS WHITE WINE 200 ml
[sku] => SKU0001
[qty] => 2
)
)
[1] => Array
(
[list_id] => 5
[order_list_name] => meri list 3
[0] => Array
(
[list_id] => 5
[product_id] => 2
[product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
[sku] => SKU0002
[qty] => 8
)
)
[2] => Array
(
[list_id] => 7
[order_list_name] => meri list 9
[0] => Array
(
[list_id] => 7
[product_id] => 2
[product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
[sku] => SKU0002
[qty] => 2
)
)
)
我期待这样的输出
<pre>Array
(
[0] => Array
(
[list_id] => 5
[order_list_name] => meri list 3
[0] => Array
(
[list_id] => 5
[product_id] => 1
[product_name] => DUCHESS WHITE WINE 200 ml
[sku] => SKU0001
[qty] => 2
),
[1] => Array
(
[list_id] => 5
[product_id] => 1
[product_name] => DUCHESS WHITE WINE 200 ml
[sku] => SKU0001
[qty] => 2
)
)
[1] => Array
(
[list_id] => 7
[order_list_name] => meri list 9
[0] => Array
(
[list_id] => 7
[product_id] => 2
[product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
[sku] => SKU0002
[qty] => 2
)
)
)
根据list_id使用临时一级密钥组。完成后,使用 array_values()
.
删除临时密钥
$grouped = [];
foreach ($records as $record) {
if (!isset($grouped[$record['list_id']])) {
$grouped[$record['list_id']] = [
'list_id' => $record['list_id'],
'order_list_name' => $record['order_list_name']
];
}
$grouped[$record['list_id']][] = [
'list_id' => $record['list_id'],
'product_id' => $product->getId(),
'product_name' => $product->getName(),
'sku' => $product->getSku(),
'qty' => $record['qty']
];
}
var_export(
array_values($grouped)
);
删除条件也会产生相同的结果;它只会默默地覆盖两个关联元素。
$grouped = [];
foreach ($records as $record) {
$grouped[$record['list_id']]['list_id'] = $record['list_id'];
$grouped[$record['list_id']]['order_list_name'] = $record['order_list_name'];
$grouped[$record['list_id']][] = [
'list_id' => $record['list_id'],
'product_id' => $product->getId(),
'product_name' => $product->getName(),
'sku' => $product->getSku(),
'qty' => $record['qty']
];
}
var_export(
array_values($grouped)
);
我花了几个小时试图找到这个问题的答案,但我很挣扎。我相当熟悉 PHP 和各种内置函数,并且可以构建一个复杂的 foreach() 循环来执行此操作,但我想我会问问是否有人对我的问题有更聪明的解决方案.
我有一个这种形式的数组,现在我想根据 list_id 匹配下一个 instance.I 将其转换为多维尝试了这个解决方案但效果不佳
<pre>Array
(
[0] => Array
(
[list_id] => 5
[order_list_name] => meri list 3
[0] => Array
(
[list_id] => 5
[product_id] => 1
[product_name] => DUCHESS WHITE WINE 200 ml
[sku] => SKU0001
[qty] => 2
)
)
[1] => Array
(
[list_id] => 5
[order_list_name] => meri list 3
[0] => Array
(
[list_id] => 5
[product_id] => 2
[product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
[sku] => SKU0002
[qty] => 8
)
)
[2] => Array
(
[list_id] => 7
[order_list_name] => meri list 9
[0] => Array
(
[list_id] => 7
[product_id] => 2
[product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
[sku] => SKU0002
[qty] => 2
)
)
)
我期待这样的输出
<pre>Array
(
[0] => Array
(
[list_id] => 5
[order_list_name] => meri list 3
[0] => Array
(
[list_id] => 5
[product_id] => 1
[product_name] => DUCHESS WHITE WINE 200 ml
[sku] => SKU0001
[qty] => 2
),
[1] => Array
(
[list_id] => 5
[product_id] => 1
[product_name] => DUCHESS WHITE WINE 200 ml
[sku] => SKU0001
[qty] => 2
)
)
[1] => Array
(
[list_id] => 7
[order_list_name] => meri list 9
[0] => Array
(
[list_id] => 7
[product_id] => 2
[product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
[sku] => SKU0002
[qty] => 2
)
)
)
根据list_id使用临时一级密钥组。完成后,使用 array_values()
.
$grouped = [];
foreach ($records as $record) {
if (!isset($grouped[$record['list_id']])) {
$grouped[$record['list_id']] = [
'list_id' => $record['list_id'],
'order_list_name' => $record['order_list_name']
];
}
$grouped[$record['list_id']][] = [
'list_id' => $record['list_id'],
'product_id' => $product->getId(),
'product_name' => $product->getName(),
'sku' => $product->getSku(),
'qty' => $record['qty']
];
}
var_export(
array_values($grouped)
);
删除条件也会产生相同的结果;它只会默默地覆盖两个关联元素。
$grouped = [];
foreach ($records as $record) {
$grouped[$record['list_id']]['list_id'] = $record['list_id'];
$grouped[$record['list_id']]['order_list_name'] = $record['order_list_name'];
$grouped[$record['list_id']][] = [
'list_id' => $record['list_id'],
'product_id' => $product->getId(),
'product_name' => $product->getName(),
'sku' => $product->getSku(),
'qty' => $record['qty']
];
}
var_export(
array_values($grouped)
);