结合2阵列不同长度的 [英] Combine 2 Arrays of Different Lengths

查看:145
本文介绍了结合2阵列不同长度的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个阵列,每个不同的罪状,我正在使用的例子是还有的在一个132和其他136

我需要 array_combine()他们(做第一个关键,而第二个值)。在我的例子,我想保持132键/值对和额外的4没有对应匹配的下降。

我目前得到这个功能(这是我在 array_combine的php.net的文档()中),但它不工作:

 函数array_combine2($ ARR1,$ ARR2){
            $ COUNT1 =计数($ ARR1);
            $ COUNT2 =计数($ ARR2);
            $ numofloops = $ COUNT2 / $ COUNT1;            $ I = 0;
            而($ I< $ numofloops){
                $ ARR3 = array_slice($ ARR2,$ COUNT1 * $ I,$ COUNT1);
                $ arr4 [] = array_combine($ ARR1,$ ARR3);
                $ I ++;
            }            返回$ arr4;
     }

我不断收到回


  

警告:array_combine()[function.array-结合]:这两个参数
  应该对与开始的行元素的数目相等
  $ arr4 [] = ...


任何意见会有所帮助,

谢谢!


解决方案

 函数array_combine2($ ARR1,$ ARR2){
    $数=分钟(计数($ ARR1),计数($ ARR2));
    返回array_combine(array_slice($ ARR1,0,$计数),array_slice($ ARR2,0,$计数));
}

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,

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.

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

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[] = ...

Any advice would help,

thanks!

解决方案

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阵列不同长度的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆