比较两个多维键值并合并非重复项
Compare two multidimensional key values & combine non duplicates
我有两个多维数组。我正在遍历两个数组,检查某些值并创建一个新数组。
$full_cats array
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
)
$sub_cats array
Array
(
[0] => Array
(
[sub_cats] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
[1] => Array
(
[sub_cats] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
[2] => Array
(
[sub_cats] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
我正在遍历两个数组以检查 $sub_cat['sub_cats']['parent'] 值是否与 $full_cat['parent_cats']['id'] 值。如果为真,则两个值都添加到 $master_cats 数组。
$master_cats = array();
foreach ($sub_cats as $sub_cat) {
foreach ($full_cats as $full_cat) {
if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){
$master_cats[] = array(
"parent_cats" => array(
$full_cat['parent_cats'],
),
"sub_cats" => array(
$sub_cat['sub_cats'],
)
);
};
};
};
$master_cats输出-
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[2] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
$master_cats 数组有两个问题。
第一个问题 - 如您所见,索引 0 和 1 具有相同的 [parent_cats] 值。如果 [parent_cats] key/values 不存在,我只想添加它们。
第二个问题 - $master_cats 数组索引 0 和 1,sub_cats 数组,一些值不同但都具有相同的 [sub_cats][parent] => 384 所以它们属于同一个数组索引,例如 0.
下面是我希望使用上面 foreach/loop 中的 $master_cats 数组实现的
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384 <-- Parent ID
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384 <-- belongs to [parent_cats][id]
)
[1] => array
(
[id] => 385
[name] => Beers
[parent] => 384 <-- belongs to [parent_cats][id]
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387 <-- Parent ID
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387 <-- belongs to [parent_cats][id]
)
)
)
)
这是一种解决方法。注意:你的数组结构有点过度嵌套......但这是给定结构的解决方案
$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
$tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
foreach ($sub_cats as $sub) {
if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
$tmp['sub_cats'][]=$sub['sub_cats'];
}
}
$all[] = $tmp;
}
print_r($all);
输出:
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
[1] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
注意:这是一个简化数组结构的示例(这需要更改我的答案中的代码,但最终可能会让您的生活更轻松)
$parent_cats= array(
array(
"id" => "384",
"name" => "Beers & Ales",
"parent" => "0"
),
array(
"id" => "387",
"name" => "Wines",
"parent" => "0"
));
我有两个多维数组。我正在遍历两个数组,检查某些值并创建一个新数组。
$full_cats array
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
)
$sub_cats array
Array
(
[0] => Array
(
[sub_cats] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
[1] => Array
(
[sub_cats] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
[2] => Array
(
[sub_cats] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
我正在遍历两个数组以检查 $sub_cat['sub_cats']['parent'] 值是否与 $full_cat['parent_cats']['id'] 值。如果为真,则两个值都添加到 $master_cats 数组。
$master_cats = array();
foreach ($sub_cats as $sub_cat) {
foreach ($full_cats as $full_cat) {
if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){
$master_cats[] = array(
"parent_cats" => array(
$full_cat['parent_cats'],
),
"sub_cats" => array(
$sub_cat['sub_cats'],
)
);
};
};
};
$master_cats输出-
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[2] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
$master_cats 数组有两个问题。
第一个问题 - 如您所见,索引 0 和 1 具有相同的 [parent_cats] 值。如果 [parent_cats] key/values 不存在,我只想添加它们。
第二个问题 - $master_cats 数组索引 0 和 1,sub_cats 数组,一些值不同但都具有相同的 [sub_cats][parent] => 384 所以它们属于同一个数组索引,例如 0.
下面是我希望使用上面 foreach/loop 中的 $master_cats 数组实现的
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384 <-- Parent ID
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384 <-- belongs to [parent_cats][id]
)
[1] => array
(
[id] => 385
[name] => Beers
[parent] => 384 <-- belongs to [parent_cats][id]
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387 <-- Parent ID
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387 <-- belongs to [parent_cats][id]
)
)
)
)
这是一种解决方法。注意:你的数组结构有点过度嵌套......但这是给定结构的解决方案
$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
$tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
foreach ($sub_cats as $sub) {
if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
$tmp['sub_cats'][]=$sub['sub_cats'];
}
}
$all[] = $tmp;
}
print_r($all);
输出:
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
[1] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
注意:这是一个简化数组结构的示例(这需要更改我的答案中的代码,但最终可能会让您的生活更轻松)
$parent_cats= array(
array(
"id" => "384",
"name" => "Beers & Ales",
"parent" => "0"
),
array(
"id" => "387",
"name" => "Wines",
"parent" => "0"
));