This question may sound silly, but I didn't find a good approach.
这个问题可能听起来很愚蠢,但我没有找到一个好方法。
I have to multidimensional arrays like this:-
我必须像这样的多维数组: -
array
(
[0] => Array
(
[field1] => A
[field2] => 100
[field3] => 20
)
[1] => Array
(
[field1] => B
[field2] => 100
[field3] => 30
)
[2] => Array
(
[field1] => C
[field2] => 100
[field3] => 30
)
)
array
(
[0] => Array
(
[field1b] => A
[field2b] => 500
[field3b] => 0
)
[1] => Array
(
[field1b] => B
[field2b] => 300
[field3b] => 10
)
)
I want to merge both arrays using field1 as Key, and having in mind that arrays aren't same size.
我想使用field1作为Key合并两个数组,并记住数组的大小不同。
The result array should be:
结果数组应该是:
array
(
[0] => Array
(
[field1] => A
[field2] => 100
[field3] => 20
[field2b] => 500
[field3b] => 0
)
[1] => Array
(
[field1] => B
[field2] => 100
[field3] => 30
[field2b] => 300
[field3b] => 10
)
[2] => Array
(
[field1] => C
[field2] => 100
[field3] => 30
)
)
Thanks in advance!
提前致谢!
4 个解决方案
#1
1
<?php
$array1 = array
(
"0" => Array
(
"field1" => "A",
"field2" => "100",
"field3" => "20"
),
"1" => Array
(
"field1" => "B",
"field2" => "100",
"field3" => "30"
),
"2" => Array
(
"field1" => "C",
"field2" => "100",
"field3" => "30"
)
);
$array2 = array
(
"0" => Array
(
"field1b" => "A",
"field2b" => "500",
"field3b" => "0"
),
"1" => Array
(
"field1b" => "B",
"field2b" => "300",
"field3b" => "10"
)
);
$new_array1 = [];
foreach ($array1 as $object1) {
$new_array1[$object1['field1']] = $object1;
}
$new_array2 = [];
foreach ($array2 as $key => $object2) {
$new_array2[$object2['field1b']] = $object2;
}
$new_array = [];
foreach($new_array1 as $key => $val)
{
$new_array[$key]['field1'] = $val['field1'];
$new_array[$key]['field2'] = $val['field2'];
$new_array[$key]['field3'] = $val['field3'];
if (array_key_exists($key,$new_array2))//Check if the key is exists in an array
{
$new_array[$key]['field2b'] = $new_array2[$key]['field2b'];
$new_array[$key]['field3b'] = $new_array2[$key]['field3b'];
}
}
print_r($new_array);
DEMO
You can try it.
你可以尝试一下。
#2
4
Try this,
尝试这个,
$array1 = array
(
"0" => Array
(
"field1" => "A",
"field2" => "100",
"field3" => "20"
),
"1" => Array
(
"field1" => "B",
"field2" => "100",
"field3" => "30"
),
"2" => Array
(
"field1" => "C",
"field2" => "100",
"field3" => "30"
)
);
$array2 = array
(
"0" => Array
(
"field1b" => "A",
"field2b" => "500",
"field3b" => "0"
),
"1" => Array
(
"field1b" => "B",
"field2b" => "300",
"field3b" => "10"
)
);
foreach($array1 as $key=>$val)
{
$new_array[$key]['field1'] = $val['field1'];
$new_array[$key]['field2'] = $val['field2'];
$new_array[$key]['field3'] = $val['field3'];
if (array_key_exists($key,$array2))//Check if the key is exists in an array
{
$new_array[$key]['field2b'] = $array2[$key]['field2b'];
$new_array[$key]['field3b'] = $array2[$key]['field3b'];
}
}
echo "<pre>";
print_r($new_array);
echo "</pre>";
DEMO
#3
3
Very simple:- apply foreach
and do it like below:-
非常简单: - 应用foreach并按以下方式执行: -
<?php
$final_array = array();
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
Output:- https://eval.in/661803
输出: - https://eval.in/661803
If your both array have different number of elements then :-
如果你的两个数组都有不同数量的元素,那么: -
<?php
$count1 = count ($arr1);
$count2 = count ($arr2);
$final_array = array();
if ($count1 >$count2){
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}else if ($count2 >$count1){
foreach ($arr2 as $key=>$val){
if(isset($arr1[$key])){
$final_array[$key] = array_merge($val,$arr1[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}else{
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}
#4
0
What about this instead defining hard-coded keys condition? all you need is to provide the key which should not be included in combined array..
那么这又定义了硬编码密钥条件呢?你需要的只是提供不应该包含在组合数组中的密钥。
演示
<?php
$A = array(
array(
'field1' => 'A',
'field2' => '100',
'field3' => '20',
),
array(
'field1' => 'B',
'field2' => 100,
'field3' => 30,
),
array(
'field1' => 'C',
'field2' => 100,
'field3' => 30,
)
);
$B = array(
array(
'field1b' => 'A',
'field2b' => 500,
'field3b' => 0
),
array(
'field1b' => 'B',
'field2b' => 300,
'field3b' => 10
)
);
$combined = array();
foreach($A as $key => $arr) {
$tempKey = reset($arr);
$second_arr = $B[ $key ];
$tempKey2 = reset($B[ $key ]);
if( $tempKey == $tempKey2 ) {
$combined[] = $arr + $second_arr;
unset( $combined[ $key ]['field1b'] );
}
else {
$combined[] = $arr;
}
}
echo '<pre>';
print_r($combined);
echo '</pre>';
#1
1
<?php
$array1 = array
(
"0" => Array
(
"field1" => "A",
"field2" => "100",
"field3" => "20"
),
"1" => Array
(
"field1" => "B",
"field2" => "100",
"field3" => "30"
),
"2" => Array
(
"field1" => "C",
"field2" => "100",
"field3" => "30"
)
);
$array2 = array
(
"0" => Array
(
"field1b" => "A",
"field2b" => "500",
"field3b" => "0"
),
"1" => Array
(
"field1b" => "B",
"field2b" => "300",
"field3b" => "10"
)
);
$new_array1 = [];
foreach ($array1 as $object1) {
$new_array1[$object1['field1']] = $object1;
}
$new_array2 = [];
foreach ($array2 as $key => $object2) {
$new_array2[$object2['field1b']] = $object2;
}
$new_array = [];
foreach($new_array1 as $key => $val)
{
$new_array[$key]['field1'] = $val['field1'];
$new_array[$key]['field2'] = $val['field2'];
$new_array[$key]['field3'] = $val['field3'];
if (array_key_exists($key,$new_array2))//Check if the key is exists in an array
{
$new_array[$key]['field2b'] = $new_array2[$key]['field2b'];
$new_array[$key]['field3b'] = $new_array2[$key]['field3b'];
}
}
print_r($new_array);
DEMO
You can try it.
你可以尝试一下。
#2
4
Try this,
尝试这个,
$array1 = array
(
"0" => Array
(
"field1" => "A",
"field2" => "100",
"field3" => "20"
),
"1" => Array
(
"field1" => "B",
"field2" => "100",
"field3" => "30"
),
"2" => Array
(
"field1" => "C",
"field2" => "100",
"field3" => "30"
)
);
$array2 = array
(
"0" => Array
(
"field1b" => "A",
"field2b" => "500",
"field3b" => "0"
),
"1" => Array
(
"field1b" => "B",
"field2b" => "300",
"field3b" => "10"
)
);
foreach($array1 as $key=>$val)
{
$new_array[$key]['field1'] = $val['field1'];
$new_array[$key]['field2'] = $val['field2'];
$new_array[$key]['field3'] = $val['field3'];
if (array_key_exists($key,$array2))//Check if the key is exists in an array
{
$new_array[$key]['field2b'] = $array2[$key]['field2b'];
$new_array[$key]['field3b'] = $array2[$key]['field3b'];
}
}
echo "<pre>";
print_r($new_array);
echo "</pre>";
DEMO
#3
3
Very simple:- apply foreach
and do it like below:-
非常简单: - 应用foreach并按以下方式执行: -
<?php
$final_array = array();
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
Output:- https://eval.in/661803
输出: - https://eval.in/661803
If your both array have different number of elements then :-
如果你的两个数组都有不同数量的元素,那么: -
<?php
$count1 = count ($arr1);
$count2 = count ($arr2);
$final_array = array();
if ($count1 >$count2){
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}else if ($count2 >$count1){
foreach ($arr2 as $key=>$val){
if(isset($arr1[$key])){
$final_array[$key] = array_merge($val,$arr1[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}else{
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}
#4
0
What about this instead defining hard-coded keys condition? all you need is to provide the key which should not be included in combined array..
那么这又定义了硬编码密钥条件呢?你需要的只是提供不应该包含在组合数组中的密钥。
演示
<?php
$A = array(
array(
'field1' => 'A',
'field2' => '100',
'field3' => '20',
),
array(
'field1' => 'B',
'field2' => 100,
'field3' => 30,
),
array(
'field1' => 'C',
'field2' => 100,
'field3' => 30,
)
);
$B = array(
array(
'field1b' => 'A',
'field2b' => 500,
'field3b' => 0
),
array(
'field1b' => 'B',
'field2b' => 300,
'field3b' => 10
)
);
$combined = array();
foreach($A as $key => $arr) {
$tempKey = reset($arr);
$second_arr = $B[ $key ];
$tempKey2 = reset($B[ $key ]);
if( $tempKey == $tempKey2 ) {
$combined[] = $arr + $second_arr;
unset( $combined[ $key ]['field1b'] );
}
else {
$combined[] = $arr;
}
}
echo '<pre>';
print_r($combined);
echo '</pre>';