Home > Backend Development > PHP Tutorial > 如何把这两个函数相同的键值相加形成新的数组

如何把这两个函数相同的键值相加形成新的数组

PHP中文网
Release: 2016-06-13 11:56:32
Original
1171 people have browsed it

  怎么把这两个函数相同的键值相加形成新的数组

<?php
$a1 = &#39;10,20,30,40;1,2,3,4&#39;;//分号无限往后扩展
$a = explode(&#39;;&#39;, $a1);
$b1 = &#39;11111;22222&#39;;//分号无限往后扩展,能与$a1对齐叠加
$b = explode(&#39;;&#39;, $b1);

print_r($a);
print_r($b);
?>
Copy after login


输出:

Array ( [0] => 10,20,30,40 [1] => 1,2,3,4 ) Array ( [0] => 11111 [1] => 22222 )
Copy after login

我想实现的是:

Array ( [0] => 10,20,30,40,11111 [1] => 1,2,3,4,22222)
Copy after login

试过array_merge_recursive() 没效果,请教高手帮帮忙!研究好几天了
------解决方案--------------------

$a1 = &#39;10,20,30,40;1,2,3,4&#39;;//分号无限往后扩展
$a = explode(&#39;;&#39;, $a1);
$b1 = &#39;11111;22222&#39;;//分号无限往后扩展,能与$a1对齐叠加

$b = explode(&#39;;&#39;, $b1);
$c = array_map(null, $a, $b);
print_r($c);
Copy after login
Array
(
    [0] => Array
        (
            [0] => 10,20,30,40
            [1] => 11111
        )

    [1] => Array
        (
            [0] => 1,2,3,4
            [1] => 22222
        )

)
Copy after login


------解决方案--------------------

$a1 = &#39;10,20,30,40;1,2,3,4&#39;;//分号无限往后扩展
$a = explode(&#39;;&#39;, $a1);
$b1 = &#39;11111;22222&#39;;//分号无限往后扩展,能与$a1对齐叠加
$b = explode(&#39;;&#39;, $b1);
 
$c=array();
foreach($a as $key=>$value){
    $c[]=$value.",".$b[$key];
}
echo &#39;

Array
(
    [0] => 10,20,30,40,11111
    [1] => 1,2,3,4,22222
)
Copy after login

以上就是如何把这两个函数相同的键值相加形成新的数组的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template