Heim  >  Artikel  >  Backend-Entwicklung  >  php如何合并多个数组成一个多维数组

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

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

在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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn