Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a php function like python's zip?

Tags:

python

php

Python has a nice zip() function. Is there a PHP equivalent?

like image 243
Craig Avatar asked May 11 '10 23:05

Craig


People also ask

What is PHP zip?

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The PHP ZipArchive class can be used to zipping and unzipping. It might be need to install the class if it is not present.

Is zip a tuple?

12.5 Lists and tupleszip is a built-in function that takes two or more sequences and “zips” them into a list of tuples where each tuple contains one element from each sequence. In Python 3, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list.

Is zip a Pythonic?

When you combine zip() , for loops, and tuple unpacking, you can get a useful and Pythonic idiom for traversing two or more iterables at once. In this example, you use zip() with three iterables to create and return an iterator that generates 3-item tuples.

What is zip doing Python?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.


1 Answers

As long as all the arrays are the same length, you can use array_map with null as the first argument.

array_map(null, $a, $b, $c, ...); 

If some of the arrays are shorter, they will be padded with nulls to the length of the longest, unlike python where the returned result is the length of the shortest array.

like image 70
habnabit Avatar answered Sep 28 '22 10:09

habnabit