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

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

WBOY
Release: 2016-06-06 20:39:15
Original
1696 people have browsed it

在Python中有zip函数,例如:

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

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

PHP如何快速实现呢

回复内容:

在Python中有zip函数,例如:

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

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

PHP如何快速实现呢

可以用 array_map 快速实现:

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

var_dump($result);
</code>
Copy after login

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>
Copy after login
Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template