Home  >  Article  >  Backend Development  >  php如何合并多个数组成一个多维数组

php如何合并多个数组成一个多维数组

WBOY
WBOYOriginal
2016-06-06 20:39:151645browse

在Python中有zip函数,例如:

<code>a = [1, 1, 1]
b = [2, 2, 2]
zip(a, b)

Out: [(1, 2), (1, 2), (1, 2)]
</code>

PHP如何快速实现呢

回复内容:

在Python中有zip函数,例如:

<code>a = [1, 1, 1]
b = [2, 2, 2]
zip(a, b)

Out: [(1, 2), (1, 2), (1, 2)]
</code>

PHP如何快速实现呢

可以用 array_map 快速实现:

<code>$a = [1, 1, 1];
$b = [2, 2, 2];
$result = array_map(null, $a, $b);

var_dump($result);
</code>

http://php.net/manual/zh/function.array-merge.php

<code>function zip($a, $b) {
    return array_map(function($x, $y){
        return [$x, $y];
    }, $a, $b);
}
</code>
Statement:
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