我有两个多维数组。我在两个数组中循环,检查某些值&创建新阵列。
$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具有相同的[pparent_cats]值。我只想在[pparent_cats]键/值不存在的情况下添加它们。
第二个问题-$master_cats数组索引0&1,sub_cats数组,有些值不同,但都具有相同的[sub_cats][parent]=>384,因此它们属于相同的数组索引,例如0。
以下是我希望使用上方foreach/循环中的$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);
示例:https://3v4l.org/01oLF
输出:
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"
));