0

I need a function that can intersect 2 arrays for example:

$Array1 = array(1,2,3);
$Array2 = array(5,6);

and results in:

array(1,5,2,6,3);

What I have so far is this

<?php
$Array1 = array(1,2,3);
$Array2 = array(5,6);

function zip() {
    $args = func_get_args();
    $zipped = array();
    $n = count($args);
    for ($i=0; $i<$n; ++$i) {
        reset($args[$i]);
    }
    while ($n) {
        $tmp = array();
        for ($i=0; $i<$n; ++$i) {
            if (key($args[$i]) === null) {
                break 2;
            }
            $tmp[] = current($args[$i]);
            next($args[$i]);
        }
        $zipped[] = $tmp;
    }
    return $zipped;
}

$bothMonths = zip($Array1, $Array2);

print_r($bothMonths);

?>

that have the output like

Array (
    [0] => Array (
        [0] => 1
        [1] => 5
    )
    [1] => Array (
        [0] => 2
        [1] => 6
    )
)

I don't know why 3 is missing.

Also I need pure programming, forget about array_merge, array_intersect ... or other functions.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Master345
  • 2,250
  • 11
  • 38
  • 50

2 Answers2

4
// Interleaves arbitrarily many input arrays. Example:
// a1 = [1, 2, 3], a2 = [5, 6], a3 = [7, 8, 9]
// zip(a1, a2, a3) => [1, 5, 7, 2, 6, 8, 3, 9]
function zip() {
  $arrays = func_get_args();
  $result = array();

  // Count the length of the arrays to get the length of the longest
  $longest = array_reduce($arrays, function($old, $e) {
      return max($old, count($e));
    }, 0);

  // Traverse the arrays, one element at a time
  for ($i = 0; $i < $longest; $i++) {
    foreach($arrays as $a) {
      if (isset($a[$i]))
        $result[] = $a[$i];
    }
  }

  return $result;
}

This assumes that your arrays are numerically indexed from 0 and up.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
2

Try this:

function custom_intersect($arr1, $arr2) {
   $len1 = count($arr1);
   $len2 = count($arr2);
   $max_len = ($len1 >= $len2) ? $len1 : $len2;

   $arr = array();
   for($i = 0; $i < $max_len; ++$i)
   {
      if(isset($arr1[$i]))
      {
         $arr[] = $arr1[$i];
      }
      if(isset($arr2[$i]))
      {
         $arr[] = $arr2[$i];
      }
   }
   return $arr;
}
Victor
  • 5,493
  • 1
  • 27
  • 28
  • i don't perfectly understand, i assume that the increment maximum is that of the biggest array, right? and also, your code is shorter then others, simple yet effective – Master345 Jun 18 '12 at 12:21
  • 1
    @RowMinds Yes, `$max_len` is the "biggest array". – Victor Jun 18 '12 at 12:23
  • Is this code really shorter, though? I count to 11 rows (not counting comments and single `{` and `}`) in both this and my answer. – Emil Vikström Jun 18 '12 at 12:27
  • @Emil Vikström your code is also good, thank you, i gave you a plus – Master345 Jun 18 '12 at 12:34
  • 1
    @EmilVikström I think, the problem is not in the number of characters. Given that he needed a pure code I tried to write example as simple possible. Anyway, you can improve your code: to determine the "biggest array" use `$longest = max(array_map('count', $arrays));` and instead of `for ($i = 0; $i < $longest; $i++) {}` use `while($longest){--$longest;}` – Victor Jun 18 '12 at 12:52
  • May be true. I just tried to clarify on the size claim. The array_map alternative is nice, I didn't remember that `max` could take an array. The `while ...` approach, however, looks like an anti-pattern since it's *harder* to read (everyone knows a standard `for` loop). – Emil Vikström Jun 18 '12 at 13:00
  • @EmilVikström About `while`, I don't agree, because: **1)** Even beginners know about `while`; and **2)** In this case `while` will be faster than `for` and this is important. – Victor Jun 18 '12 at 13:25
  • 1
    Yes, everybody knows about both `for` and `while`, but a `for` can be immediately spotted by an experienced programmer (all `for` loops can be expressed as `while`, but they were added to C and other languages as syntactic sugar for a very common pattern). The speed in this is so much microoptimization that it's just absurd to even talk about it, especially when somebody chose PHP to begin with. So no, this is not important. Maintainability is important. Besides, you are using the same `for` loop as me in your answer, probably because *it's a good use of a common construct*. Stop trolling :-) – Emil Vikström Jun 18 '12 at 13:32
  • @EmilVikström Emil, I'm not trolling. After all, it was about your example and how to make it shorter. And shorter, means better. Anyway, I'm totally agree with you. – Victor Jun 18 '12 at 13:50