在一个数组中有键,在另一个数组中有值。我想以如下方式组合这两个数组,即array1的所有键和array2的所有值。如何在PHP中完成?
这是两个数组
Array
(
[0] => url
[1] => downloadName
[2] => downloadType
[3] => downloadSize
[4] => url
[5] => downloadName
[6] => downloadType
[7] => downloadSize
)
Array
(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[2] => Manual
[3] => 0.39
[4] => https://www.clearcube.com/support/controller/downloads.php?id=582
[5] => G0400147 Rev B.xlsx
[6] => Manual
[7] => 0.37
)
我需要这种格式。
Array
(
[url] => Array(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => https://www.clearcube.com/support/controller/downloads.php?id=582
)
[downloadName] => Array(
[0] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[1] => G0400147 Rev B.xlsx
)
[downloadType] => Array(
[0] => Manual
[1] => Manual
)
[downloadSize] => Array(
[0] => 0.39
[1] => 0.37
)
)
答案 0 :(得分:4)
借助array_walk(),您可以轻松地将其合并。
$keys = ['url','downloadName','downloadType','downloadSize','url','downloadName','downloadType','downloadSize'];
$values = [ 'https://www.clearcube.com/support/controller/downloads.php?id=450', 'F6151 Media Converter System with 100 base TX to 100 base FX.pdf', 'Manual', 0.39, 'https://www.clearcube.com/support/controller/downloads.php?id=582', 'G0400147 Rev B.xlsx', 'Manual', 0.37 ];
$result = [];
array_walk($keys, function ($val, $key) use (&$result, $values) { $result[$val][] = $values[$key]; });
print_r($result);
工作demo。
输出:
Array
(
[url] => Array
(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => https://www.clearcube.com/support/controller/downloads.php?id=582
)
[downloadName] => Array
(
[0] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[1] => G0400147 Rev B.xlsx
)
[downloadType] => Array
(
[0] => Manual
[1] => Manual
)
[downloadSize] => Array
(
[0] => 0.39
[1] => 0.37
)
)
答案 1 :(得分:2)
一种简单的方法是
$result = [];
foreach ($array1 as $key1 => $value1) {
$result[$value1][] = $array2[$key1];
}
已实施验证并且独立于索引
if (count($array1) == count($array2)) {
$result = [];
$i = 0;
foreach ($array1 as $value1) {
$j=0;
foreach ($array2 as $value2) {
if ($j == $i) {
$result[$value1][] = $value2;
break;
}
$j++;
}
$i++;
}
var_dump($result);
}
答案 2 :(得分:0)
假设键数组是$ arr1,数据数组是$ arr2,那么您可以运行以下循环并通过以下操作创建新数组
$new_arr = []
foreach($arr1 as $key=>$arr){
$new_arr[$arr][] = $arr2[$key];
}
//final array is $new_arr
答案 3 :(得分:0)
您可能会使用基于索引的方法,首先检查数组的长度是否相同。
从数组1中找到键,并为该键添加数组2中的值。
$result = [];
if (count($array1) === count($array2)) {
for ($i = 0; $i < count($array1); $i++) {
$result[$array1[$i]][] = $array2[$i];
}
}
print_r($result);
结果
Array
(
[url] => Array
(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => https://www.clearcube.com/support/controller/downloads.php?id=582
)
[downloadName] => Array
(
[0] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[1] => G0400147 Rev B.xlsx
)
[downloadType] => Array
(
[0] => Manual
[1] => Manual
)
[downloadSize] => Array
(
[0] => 0.39
[1] => 0.37
)
)