Is There a PHP Function Like Python'S Zip

Is there a php function like python's zip?

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.

Does ruby have a zip function like python's?

That is what Array#zip does:

foo = [1,2,3,4]
bar = ['a','b','c','d']

foo.zip(bar) #=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]

Mixing two arrays together in PHP

You can use array_map function providing your two arrays in parameter :

<?php
$array1=Array("one", "two", "three");
$array2=Array("four", "five", "six");

$res=array_map(function($r1, $r2) {return "$r1 $r2";}, $array1, $array2);
print_r($res);

Result

Array
(
[0] => one four
[1] => two five
[2] => three six
)

Python's equivalent to PHP's strip_tags?

There is no such thing in the Python standard library. It's because Python is a general purpose language while PHP started as a Web oriented language.

Nevertheless, you have 3 solutions:

  • You are in a hurry: just make your own. re.sub(r'<[^>]*?>', '', value) can be a quick and dirty solution.
  • Use a third party library (recommended because more bullet proof) : beautiful soup is a really good one and there is nothing to install, just copy the lib dir and import. Full tuto with beautiful soup.
  • Use a framework. Most Web Python devs never code from scratch, they use a framework such as django that does automatically this stuff for you. Full tuto with django.

PHP: Take several arrays, and make new ones based on shared indexes?

Mostly like Alex Barrett's answer, but allows for an unknown number of arrays.

<?php

$values = array(
array(1,2,3,4,5),
array(6,7,8,9,10),
array(11,12,13,14,15),
);

function array_pivot($values)
{
array_unshift($values, null);
return call_user_func_array('array_map', $values);

}

print_r(array_pivot($values));

Help with php function zip_open

Use ZipArchive::getFromName(). Example from the PHP manual adapted to your case:

$zip = new ZipArchive();
if ($zip->open($_FILES["restore_file"]["tmp_name"]) === true) {
echo $zip->getFromName('example/index.php');
$zip->close();
} else {
echo 'failed';
}


Related Topics



Leave a reply



Submit