[英]Combining multiple array values in PHP
I have three arrays right now the values of which I want to combine together. 我现在有三个数组,我想将它们的值组合在一起。 All of the values have matching keys but I can't work out quite how to do it.
所有的值都有匹配的键,但是我不知道该怎么做。 To put it more visually I have:
更直观地说,我有:
array{
[0] => "Foo"
}
array{
[0] => " Bar"
}
and I want: 而且我要:
array{
[0] => "Foo Bar"
}
But for the life of me I can't figure out how! 但是对于我的一生,我不知道怎么做! At first I thought about using nested foreach statements like
起初,我考虑过使用嵌套的foreach语句,例如
$result = array();
foreach ($array1 as &$input1) {
foreach ($array2 as &$input2) {
$result[] = $input1 . $input2;
}
}
But while that combined the values, it generated a lot of correct ones (The array was about twice the size as expected). 但是,尽管将这些值组合在一起,却生成了许多正确的值(该数组的大小约为预期的两倍)。
Use the keys 使用按键
$output = array();
foreach (array_keys($array1) as $key) {
$output[] = $array1[$key] . $array2[$key]; // and possibly . $array3[$key]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.