合并 2 个不同的数组并在 php 中形成一个数组
Merge 2 different array and form a single array in php
我需要合并两个不同的数组并形成一个数组。关键字段在两个数组中具有相同的值。
$array1=Array (
[0] => Array ( [key] => 39 [url] => www.youtube.com/embed/23sdg234 )
[1] => Array ( [key] => 39 [url] => player.vimeo.com/video/s3423sfsdf )
[2] => Array ( [key] => 39 [url] => www.youtube.com/embed/wersd2323 )
[3] => Array ( [key] => 40 [url] => www.youtube.com/embed/3423sfsd )
[4] => Array ( [key] => 40 [url] => www.youtube.com/embed/asd23423 )
)
array1 需要根据 key=key1
从 array2 中获取值 [firstName],[lastName]
$array2=Array (
[0] => Array ( [key1] => 39 [firstName] => xyz [lastName] => s )
[1] => Array ( [key1] => 40 [firstName] => abc [lastName] => e )
)
结果应该形成如下数组。
$result=Array (
[0] => Array ( [key] => 39 [firstName] => xyz [lastName] => s [url] => www.youtube.com/embed/23sdg234 )
[1] => Array ( [key] => 39 [firstName] => xyz [lastName] => s [url] => player.vimeo.com/video/s3423sfsdf )
[2] => Array ( [key] => 39 [firstName] => xyz [lastName] => s [url] => www.youtube.com/embed/wersd2323 )
[3] => Array ( [key] => 40 [firstName] => abc [lastName] => e [url] => www.youtube.com/embed/3423sfsd )
[4] => Array ( [key] => 40 [firstName] => abc [lastName] => e [url] => www.youtube.com/embed/asd23423 )
)
在此先感谢您。
<?php
foreach($array1 as $vid)
{
$found = false;
foreach ($array2 as $user)
{
if ($vid['key'] === $user['key1'])
{
$found = true;
break 1;
}
}
if ($found)
{
$result[] = array(
'key' => $vid['key'] ,
'firstname' => $user['firstname'] ,
'lastname' => $user['lastname'] ,
'url' => $array1['url']
);
}
else
{
$result[] = array(
'key' => $vid['key'] ,
'firstname' => "Unknown" ,
'lastname' => "Unknown" ,
'url' => $array1['url']
);
}
}
还没有测试过,但应该可以。
它基本上循环遍历 Array1 中的每个项目。然后对于那里的每个项目,它检查 Array2 中是否存在相同的键。然后,如果键匹配,它会将所有数据写入一个新数组。如果未找到匹配的键,则名称字段将填充 "unknown"。这只是解决了基本问题,您可能希望根据您的需要对其进行调整。
朴素的方式:
foreach ($array1 as &$values) {
foreach ($array2 as $mixin) {
if ($mixin['key'] == $values['key']) {
$values += $mixin;
}
}
}
花哨的、更优化的方式:
$mixins = array_combine(array_column($array2, 'key'), $array2);
$result = array_map(
function (array $values) use ($mixins) { return $values += $mixins[$values['key']]; },
$array1
);
遍历第一个数组,并创建一个组合数组:
$combinedArray = [];
array_walk(function($val,$key) use ($array1,$combinedArray){
if(array_key_exists($key,$array1))
{
$combinedArray[$key] = array_merge($val,$array1[$key]);
}
else
{
$combinedArray[$key] = $val;
}
},$array);
$array1=Array (
0 => Array ( 'key' => 39, 'url' => "www.youtube.com/embed/23sdg234" ),
1 => Array ( 'key' => 39, 'url' => "player.vimeo.com/video/s3423sfsdf" ),
2 => Array ( 'key' => 39, 'url' => "www.youtube.com/embed/wersd2323" ),
3 => Array ( 'key' => 40, 'url' => "www.youtube.com/embed/3423sfsd" ),
4 => Array ( 'key' => 40, 'url' => "www.youtube.com/embed/asd23423" ),
);
$array2=Array (
0 => Array ( 'key1' => 39, 'firstName' => 'xyz', 'lastName' => 's' ),
1 => Array ( 'key1' => 40, 'firstName' => 'abc', 'lastName' => 'e' ),
);
$result=array();
foreach($array1 as $da){
$fName='';
$lName='';
foreach($array2 as $da2){
if($da['key']==$da2['key1']){
$fName=$da2['firstName'];
$lName=$da2['lastName'];
break;
}
}
$result[]=array(
'key'=>$da['key'],
'firstName'=>$fName,
'lastName'=>$lName,
'url'=>$da['url'],
);
}
print_r($result);
我需要合并两个不同的数组并形成一个数组。关键字段在两个数组中具有相同的值。
$array1=Array (
[0] => Array ( [key] => 39 [url] => www.youtube.com/embed/23sdg234 )
[1] => Array ( [key] => 39 [url] => player.vimeo.com/video/s3423sfsdf )
[2] => Array ( [key] => 39 [url] => www.youtube.com/embed/wersd2323 )
[3] => Array ( [key] => 40 [url] => www.youtube.com/embed/3423sfsd )
[4] => Array ( [key] => 40 [url] => www.youtube.com/embed/asd23423 )
)
array1 需要根据 key=key1
从 array2 中获取值 [firstName],[lastName]$array2=Array (
[0] => Array ( [key1] => 39 [firstName] => xyz [lastName] => s )
[1] => Array ( [key1] => 40 [firstName] => abc [lastName] => e )
)
结果应该形成如下数组。
$result=Array (
[0] => Array ( [key] => 39 [firstName] => xyz [lastName] => s [url] => www.youtube.com/embed/23sdg234 )
[1] => Array ( [key] => 39 [firstName] => xyz [lastName] => s [url] => player.vimeo.com/video/s3423sfsdf )
[2] => Array ( [key] => 39 [firstName] => xyz [lastName] => s [url] => www.youtube.com/embed/wersd2323 )
[3] => Array ( [key] => 40 [firstName] => abc [lastName] => e [url] => www.youtube.com/embed/3423sfsd )
[4] => Array ( [key] => 40 [firstName] => abc [lastName] => e [url] => www.youtube.com/embed/asd23423 )
)
在此先感谢您。
<?php
foreach($array1 as $vid)
{
$found = false;
foreach ($array2 as $user)
{
if ($vid['key'] === $user['key1'])
{
$found = true;
break 1;
}
}
if ($found)
{
$result[] = array(
'key' => $vid['key'] ,
'firstname' => $user['firstname'] ,
'lastname' => $user['lastname'] ,
'url' => $array1['url']
);
}
else
{
$result[] = array(
'key' => $vid['key'] ,
'firstname' => "Unknown" ,
'lastname' => "Unknown" ,
'url' => $array1['url']
);
}
}
还没有测试过,但应该可以。 它基本上循环遍历 Array1 中的每个项目。然后对于那里的每个项目,它检查 Array2 中是否存在相同的键。然后,如果键匹配,它会将所有数据写入一个新数组。如果未找到匹配的键,则名称字段将填充 "unknown"。这只是解决了基本问题,您可能希望根据您的需要对其进行调整。
朴素的方式:
foreach ($array1 as &$values) {
foreach ($array2 as $mixin) {
if ($mixin['key'] == $values['key']) {
$values += $mixin;
}
}
}
花哨的、更优化的方式:
$mixins = array_combine(array_column($array2, 'key'), $array2);
$result = array_map(
function (array $values) use ($mixins) { return $values += $mixins[$values['key']]; },
$array1
);
遍历第一个数组,并创建一个组合数组:
$combinedArray = [];
array_walk(function($val,$key) use ($array1,$combinedArray){
if(array_key_exists($key,$array1))
{
$combinedArray[$key] = array_merge($val,$array1[$key]);
}
else
{
$combinedArray[$key] = $val;
}
},$array);
$array1=Array (
0 => Array ( 'key' => 39, 'url' => "www.youtube.com/embed/23sdg234" ),
1 => Array ( 'key' => 39, 'url' => "player.vimeo.com/video/s3423sfsdf" ),
2 => Array ( 'key' => 39, 'url' => "www.youtube.com/embed/wersd2323" ),
3 => Array ( 'key' => 40, 'url' => "www.youtube.com/embed/3423sfsd" ),
4 => Array ( 'key' => 40, 'url' => "www.youtube.com/embed/asd23423" ),
);
$array2=Array (
0 => Array ( 'key1' => 39, 'firstName' => 'xyz', 'lastName' => 's' ),
1 => Array ( 'key1' => 40, 'firstName' => 'abc', 'lastName' => 'e' ),
);
$result=array();
foreach($array1 as $da){
$fName='';
$lName='';
foreach($array2 as $da2){
if($da['key']==$da2['key1']){
$fName=$da2['firstName'];
$lName=$da2['lastName'];
break;
}
}
$result[]=array(
'key'=>$da['key'],
'firstName'=>$fName,
'lastName'=>$lName,
'url'=>$da['url'],
);
}
print_r($result);