我在解决将数组合并到多维数组方面遇到麻烦。
我的数组如下:
Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_Sinoskysun International (HK) CO., LTD
)
[1] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_RUTRONIK Elektronische Ba
)
[2] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TTI
)
[3] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_ARROW CENTRAL EUROPE GMBH
)
[4] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
[5] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_YANGZHIDING ELECTRONICS
)
[6] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[7] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)
我需要像这样合并它们:
Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)
输入数组可以是5或500个项目,我需要通过ID进行合并。.彼此之间只有一件事不同,它是由谁制造的。有可能吗?
如果需要更多,我可以添加。 :)
非常感谢您的帮助。
答案 0 :(得分:1)
此代码将产生所需的输出,而不管数组是否具有不同的id值。此代码确实假定所有相似的产品均按顺序分组:
$output = array();
$i = -1;
foreach ($array as $arr) {
if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
// new product
$output[++$i] = $arr;
$output[$i]['madeBy2'] = array($arr['madeBy2']);
}
else {
// same product, new manufacturer
$output[$i]['madeBy2'][] = $arr['madeBy2'];
}
}
print_r($output);
备用版本:如果有多个值,则仅将madeBy2
元素放入数组:
$output = array();
$i = -1;
foreach ($array as $arr) {
if ($i < 0 || $output[$i]['id'] != $arr['id'] || $output[$i]['regNumber'] != $arr['regNumber'] || $output[$i]['name'] != $arr['name']) {
// new product
$output[++$i] = $arr;
}
else {
// same product, new manufacturer
if (!is_array($output[$i]['madeBy2']))
$output[$i]['madeBy2'] = array($output[$i]['madeBy2'], $arr['madeBy2']);
else
$output[$i]['madeBy2'][] = $arr['madeBy2'];
}
}
print_r($output);
输出(用于示例数据)
Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
"Pro_Sinoskysun International (HK) CO., LTD",
"Pro_RUTRONIK Elektronische Ba",
"Pro_TTI",
"Pro_ARROW CENTRAL EUROPE GMBH",
"Pro_TDK",
"Pro_YANGZHIDING ELECTRONICS"
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] =>
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Pro_TDK
)
)
答案 1 :(得分:0)
我建立了一个数组,该数组由数据中的id
值索引,如果尚未设置,那么我将用数据创建一个新元素-尽管madeBy2
被创建为数组。如果元素已经存在,则将新值添加到现有值列表中(使用[]
)...
$out = [];
foreach ( $inp as $data ) {
if ( isset ($out[$data['id']])) {
$out[$data['id']]["madeBy2"][] = $data["madeBy2"];
}
else {
$out[$data['id']] = [ "id"=>$data['id'],
"regNumber" => $data['regNumber'],
"name" => $data['name'],
"madeBy2" => [$data['madeBy2']]];
}
}
print_r(array_values($out));
这将留下一个由id
索引的数组,因此我在array_values()
中使用print_r()
,可以根据需要将其保留。
答案 2 :(得分:0)
代码注释
foreach ($array as $arr) {
if(!isset($output[$arr["id"]])){ // if item does not exist
$output[$arr["id"]] = $arr; // create array
$output[$arr["id"]]['madeBy2'] = [$output[$arr["id"]]['madeBy2']]; // change type on madeBy2 to array
}else{
$output[$arr["id"]]['madeBy2'][] = $arr['madeBy2']; // it item exist add madeBy2 to the list
}
}
$output = array_values($output); // remove associative array keys.
print_r($output);
此方法不依赖于要排序的列表,只有madeBy2是硬编码。
意味着如果一个数组中有一个额外的项目,则此方法将使其与Nigel的项目相反。
输出:
Array
(
[0] => Array
(
[id] => 41844
[regNumber] => CSM-057.05
[name] => 100nSMD060310%X7R25V
[madeBy2] => Array
(
[0] => Pro_SinoskysunInternational(HK) CO.,LTD
[1] => Pro_RUTRONIKElektronischeBa
[2] => Pro_TTI
[3] => Pro_ARROWCENTRALEUROPEGMBH
[4] => Pro_TDK
[5] => Pro_YANGZHIDINGELECTRONICS
)
)
[1] => Array
(
[id] => 41301
[regNumber] => P+CSM-302.01
[name] => 100n SMD 0603 5% X7R 25V
[madeBy2] => Array
(
[0] =>
)
)
[2] => Array
(
[id] => 24526
[regNumber] => P+CSM-097.04
[name] => 100n SMD 0603 10% X7R 25V
[madeBy2] => Array
(
[0] => Pro_TDK
)
)
)
带有额外项目的演示:
https://3v4l.org/0jmpL