我有两个数组
的print_r($ VAL);
的print_r($结果);
我需要通过PERSONAL ID连接这两个数组,例如array1。 123456到array2。 123456,也是array1 654321到array2 654321
数组1:
Array (
[1] => Array
(
[0] => test1
[1] => 123456
)
[2] => Array
(
[0] => test2
[1] => 654321
)
)
数组2:
Array (
[5] => Array
(
[login] => 123456
[firstname] => George
[lastname] => George
)
[8] => Array
(
[personal_id] => 654321
[firstname] => John
[lastname] => John
)
如何加入这两个阵列?
谢谢
答案 0 :(得分:1)
试试这个。
$new_Array = array();
foreach ($val as $key => $v) {
foreach ($results as $r) {
if($v[1] == $r['login']){
$new_Array[$key]['login'] = $r['login'];
$new_Array[$key]['firstname'] = $r['firstname'];
$new_Array[$key]['lastname'] = $r['lastname'];
$new_Array[$key]['test'] = $v[0];
}
}
}
print_r($new_Array);
答案 1 :(得分:1)
$firstArray = [
1 => [
0 => 'test1',
1 => '123456',
],
2 => [
0 => 'test2',
1 => '654321',
]
];
// move index to keys to avoid second-dimension foreach in future
$idxCachedArray = [];
foreach ($firstArray as $item) {
$idxCachedArray[$item[1]] = $item;
unset ($idxCachedArray[$item[1]][1]);
}
$secondArray = [
5 => [
'login' => 123456,
'firstname' => 'George',
'lastname' => 'George',
],
8 => [
'login' => 654321,
'firstname' => 'John',
'lastname' => 'John',
],
];
$resultArray = [];
foreach ($secondArray as $key => $item) {
$resultArray[$key] = $item;
if (isset($idxCachedArray[$item['login']])) {
$resultArray[$key] = array_merge($resultArray[$key], $idxCachedArray[$item['login']]);
}
}
var_dump ($resultArray);
答案 2 :(得分:1)
你可以做一些这类事情
$arr1 = [
['test', '123'],
['test2', '345']
];
$arr2 = [
[
'login' => '123',
'first' => 'asddf'
], [
'login' => '345',
'first' => 'def'
]
];
$mergedArray = [];
foreach ($arr1 as $elem) {
$login = $elem[1];
$match = null;
foreach ($arr2 as $elem2) {
if ($elem2['login'] == $login) {
$match = $elem2;
}
}
$mergedArray[] = array_merge($elem, $match);
}
print_r($mergedArray);
答案 3 :(得分:1)
尝试这样的事情......
$val = array (
'1' => array(
'0' => 'test1',
'1' => '123456',
),
'2' => array(
'0' => 'test2',
'1' => '654321',
)
);
$results = array (
'5' => array
(
'login' => '123456',
'firstname' => 'George',
'lastname' => 'George',
),
'8' => array(
'personal_id' => '654321',
'firstname' => 'John',
'lastname' => 'John',
)
);
foreach ($val as $key => $value) {
$val = $value['1'];
foreach ($results as $key1 => $value1) {
if ($value1['login'] == $val) {
$output = array_merge($value,$value1);
} elseif ($value1['personal_id'] == $val) {
$output2 = array_merge($value,$value1);
}
}
}
echo "<pre>";
print_r(array_unique($output));
echo "<pre>";
print_r(array_unique($output2));
答案 4 :(得分:0)
您没有清楚地输出,假设您预期的输出是这样的:
array(2) {
[123456]=>
array(4) {
["firstname"]=>
string(6) "George"
["lastname"]=>
string(6) "George"
["personal_id"]=>
int(123456)
[0]=>
string(5) "test1"
}
[654321]=>
array(4) {
["firstname"]=>
string(4) "John"
["lastname"]=>
string(4) "John"
["personal_id"]=>
int(654321)
[0]=>
string(5) "test2"
}
}
这是代码,demo,希望它有所帮助。
<?php
$val = array (
'1' => array(
'0' => 'test1',
'1' => '123456',
),
'2' => array(
'0' => 'test2',
'1' => '654321',
)
);
$results = array (
'5' => array
(
'login' => '123456',
'firstname' => 'George',
'lastname' => 'George',
),
'8' => array(
'personal_id' => '654321',
'firstname' => 'John',
'lastname' => 'John',
)
);
/*
$indexOfPrimarykey is the nth of index as the specify id, to make it more universely, I used index.
You array is just the first and the last index, here also can use
reset($array); $first_key = key($array); to get the first key,
and
end($array); $last_key = key($array); to get the last key.
*/
function mergeArray($array, $indexOfPrimarykey)
{
$o = [];
array_walk($array, function($v) use($indexOfPrimarykey, &$o)
{
$keys = array_keys($v);
$k = $keys[$indexOfPrimarykey];
$value = $v[$k];
unset($v[$k]);
$o[$value] = $v;
});
return $o;
}
$val = mergeArray($val,1);
$results = mergeArray($results,0);
array_walk($val, function($v, $k) use(&$results)
{
$results[$k] += ['personal_id' => $k] + $v;
});
var_dump($results);