组合2个不同长度的数组[复制]

时间:2021-07-06 20:24:34

This question already has an answer here:

这个问题已经有了答案:

I have two arrays, each of different counts, the example I'm working with is there's 132 in one and 136 in the other,

我有两个数组,每个数都不同,我用的例子是132在1和136之间,

I need to array_combine() them (make the first one the key, and the second one the value). In my example I would like to keep 132 key/value pairs and drop of the extra 4 that have no corresponding match.

我需要array_combine()它们(将第一个作为键,第二个作为值)。在我的示例中,我希望保留132对键/值对,并删除没有相应匹配的额外的4。

I have currently got this function (which I found on php.net's docs of array_combine()), but it isn't working:

我目前有这个函数(我在php.net的array_combine()文档中找到的),但它不起作用:

function array_combine2($arr1, $arr2) {
            $count1 = count($arr1);
            $count2 = count($arr2);
            $numofloops = $count2/$count1;

            $i = 0;
            while($i < $numofloops){
                $arr3 = array_slice($arr2, $count1*$i, $count1);
                $arr4[] = array_combine($arr1,$arr3);
                $i++;
            }

            return $arr4;
     }

I keep getting back

我越来越

Warning: array_combine() [function.array-combine]: Both parameters should have an equal number of elements on the line that starts with $arr4[] = ...

警告:合二为一()函数。array-combine]:两个参数在以$arr4[] =…

Any advice would help,

任何建议将帮助,

thanks!

谢谢!

3 个解决方案

#1


11  

function array_combine2($arr1, $arr2) {
    $count = min(count($arr1), count($arr2));
    return array_combine(array_slice($arr1, 0, $count), array_slice($arr2, 0, $count));
}

#2


9  

Here is a one-liner:

这里是一个小笑话:

$res = array_combine(array_intersect_key($arr1, $arr2), array_intersect_key($arr2, $arr1));

#3


-2  

here is perfect function in PHP itself called "array_combine()"

PHP中有一个叫做“array_combine()”的完美函数

  /*-----------------------------------------------------------
  | Parameter            | Description                        |
  -------------------------------------------------------------
  | array1  Required.   | An array, specifying the keys       |
  -------------------------------------------------------------
  | array2  Required.   | An array, specifying the values     |
  -----------------------------------------------------------*/

$array1 = ('John','Mark','Ester');
$array2 = (1111,2222);

// if some elements dont exists, "add" them...
if(count($array1) != count($array2))
{
  foreach($array as $key => $value):
      if(!isset($array2[$key]) $array2[$key] = NULL;
  endforeach;
}

// now, combine them in classic way...
$combined = array_combine($array1,$array2);

#1


11  

function array_combine2($arr1, $arr2) {
    $count = min(count($arr1), count($arr2));
    return array_combine(array_slice($arr1, 0, $count), array_slice($arr2, 0, $count));
}

#2


9  

Here is a one-liner:

这里是一个小笑话:

$res = array_combine(array_intersect_key($arr1, $arr2), array_intersect_key($arr2, $arr1));

#3


-2  

here is perfect function in PHP itself called "array_combine()"

PHP中有一个叫做“array_combine()”的完美函数

  /*-----------------------------------------------------------
  | Parameter            | Description                        |
  -------------------------------------------------------------
  | array1  Required.   | An array, specifying the keys       |
  -------------------------------------------------------------
  | array2  Required.   | An array, specifying the values     |
  -----------------------------------------------------------*/

$array1 = ('John','Mark','Ester');
$array2 = (1111,2222);

// if some elements dont exists, "add" them...
if(count($array1) != count($array2))
{
  foreach($array as $key => $value):
      if(!isset($array2[$key]) $array2[$key] = NULL;
  endforeach;
}

// now, combine them in classic way...
$combined = array_combine($array1,$array2);