douzhang7603 2012-10-22 17:18
浏览 27

确定数组是否包含另一个数组

This is the third time that i'm posting about this topic. User Baba helped me everytime. Since the SO website has not a user message system -unfortunately- I'm asking a question within a post. Again.

With the help of the user Baba, we have managed to create a function that checks whether an array contains another array, but the function determines it multiple times and creates an index list. What the function lacks and I need to know is that the function does not return -1 in the exceptions like expected. Function may return values that are not related, and the function is not consistent. We simply need to check which arrays are matched with the main array($leftArray) and return the index's of those arrays. If there is not any matched array, the function have to return -1.

Please review this code and help me:

<?php
$leftArray = array(7,6,14,15,8,0,1,4,5,9); 
//contains $GroupOfFour[6] and $GroupOfFour[1], and some excess
//numbers. Function should return array(6,1), If there is not a
//matching case the function should return -1.
//i've realised that the exception cases and the multiple
//grouping does not work.

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

function searchFourTerms($leftArray, $GroupOfFour) {
    global $GroupOfFour, $leftArray;
    $len4 = count($leftArray);
    $len4_carry = count($leftArray);
    $list4 = array();
    for($i4 = 0; $i4 < count($GroupOfFour); $i4 ++) {
        $intercept4 = array_intersect($GroupOfFour[$i4], $leftArray);
        $len4 = count($intercept4);
        if (count($intercept4) % 4 == 0) {
            $list4[$i4] = $len4;
        }
    }
    arsort($list4);
    if (empty($list4) || ($len4_carry<4))
        return - 1;
    return key($list4);
}

?>
  • 写回答

2条回答 默认 最新

  • doupuxuan5784 2012-10-22 18:04
    关注

    key($list4) will only return the current index of list4. In your case it would return 18.

    Try this, it worked for me. Instead of returning key($list4) I returned an array of matching indexes.

    function searchFourTerms($la, $gof) {
    $i3=0;
    if(count($la)<4){
        return -1;
    }
    $list4 = array();
    for($i4 = 0; $i4 < count($gof); $i4++) {
        $intercept4 = array_intersect($gof[$i4], $la);
        $len4 = count($intercept4);
        if(count($intercept4)==4) {
            $list4[$i3] = $i4;
            $i3++;
        }
    }
    if (empty($list4)){
        return - 1;
    }
    $list5= array();
    $i7=0;
    for($i4=0; $i4<count($list4); $i4++){
        $i6=0;
        for($i5=0; $i5<count($list4); $i5++){
            if($i4!=$i5){
                $i6+=count(array_intersect($gof[$i4], $gof[$i5]));
            }
        }
        if($i6<count($gof[$i4])){
            $list5[$i7]=$list4[$i4];
            $i7++;
        }
    }
    return $list5;
    }
    

    if $leftArray = array(0,1,3,2,7,6,8,9), doing a var_dump on the returned array prints the following array, which excludes [13] since it contains all duplicates from [0] and [18]:

    array(2) {
    [0]=>
    int(0)
    [1]=>
    int(18)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算