我有一个对象数组,那些对象都是同一个对象。但最后我需要将对象的所有值都放在单个对象中。这些是我的对象。
Array ( [0] => Permissions Object ( [modules] => Array ( [0] => 3 [1] => 4 ) [submodules] => Array ( ) [operations] => Array ( [3M] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [4M] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) ) ) [1] => Permissions Object ( [modules] => Array ( [0] => 1 [1] => 3 ) [submodules] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 7 [5] => 8 ) [operations] => Array ( [1] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [2] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [3] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [4] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [7] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [8] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) [3M] => Array ( [0] => Create [1] => Read [2] => Update [3] => Delete ) ) ) )
答案 0 :(得分:0)
在合并之前或之后,您只需要删除重复的值。
$array1 = [
"modules" => [
0 => 2, 1 => 4
],
"submodules" => [
"3m" => [
0 => "create", 1 => "read"
],
"4m" => [0 => "create", 1 => "read"
]
]
];
$array2 = [
"modules" => [
0 => 1,
1 => 3
],
"submodules" => [
"1m" => [
0 => "create", 1 => "read"
],
"10m" => [
0 => "create", 1 => "read"
]
]
];
$merged = array_merge_recursive($array1, $array2);
print_r($merged);
Array ( [modules] => Array (
[0] => 2 [1] => 4 [2] => 1 [3] => 3
)
[submodules] => Array (
[3m] => Array ( [0] => create [1] => read
)
[4m] =>
Array ( [0] => create [1] => read
)
[1m] => Array (
[0] => create [1] => read
)
[10m] => Array (
[0] => create [1] => read
)
)
)
答案 1 :(得分:0)
如果您不想使用array_merge,可以按如下方式使用解决方法:
$global; //your array
$merged = array(); //the array you want with the merged objects
foreach ($global as $k => $object) {
foreach ($object as $key => $values) {
foreach ($values as $val)
$merged[$key][] = $val;
}
}