比较基于数组PHP的前N个元素的两个数组

时间:2013-11-25 04:01:08

标签: php arrays mysqli

我试图根据每个数组的前三个元素来比较两个数组。然后,我将使用array_udiff来查找第一个原始数组中不在第二个原始数组中的元素。这是我的代码和当前输出

function udiffanswers($a,$b){
    $slicea = array_slice($a,0,3);  
    $sliceb = array_slice($b,0,3);  
    print_r($slicea);
    print_r($sliceb);
    if($slicea == $sliceb)
    {
        return 0;
    }else{
        return -1;
    }
}

$diffinanswers = array_udiff($row ,$answers, 'udiffanswers');

echo "diff results \n"; 
print_r($diffinanswers);

这是我的输入

 answers sqlite results 
 Array
  (
  [0] => Array
    (
        [aid] => 432267
        [qid] => 0
        [sid] => 1
        [freetext] => 
        [a] => Dog
        [b] => null 1
        [c] => null 2
        [d] => null 3
        [e] => null 4
        [f] => null 5
        [g] => null 6
        [h] => null 7
        [i] => null 8
        [j] => null 9
        [type] => Multiple Choice
        [location] => 37.334709,-122.034111
    )
 [1] => Array
    (
        [aid] => 1
        [qid] => 0
        [sid] => 1
        [freetext] => 
        [a] => Dog
        [b] => null 1
        [c] => null 2
        [d] => null 3
        [e] => null 4
        [f] => null 5
        [g] => null 6
        [h] => null 7
        [i] => null 8
        [j] => null 9
        [type] => Multiple Choice
        [location] => 37.334709,-122.034111
    )
   )

回答mysqli结果

 Array
 (
 [0] => Array
    (
        [aid] => 42
        [qid] => 0
        [sid] => 1
        [freetext] => 
        [a] => dog
        [b] => 
        [c] => 
        [d] => 
        [e] => 
        [f] => 
        [g] => 
        [h] => 
        [i] => 
        [j] => 
        [type] => 
        [location] => 
    )

   [1] => Array
    (
        [aid] => 432267
        [qid] => 0
        [sid] => 1
        [freetext] => 
        [a] => Dog
        [b] => null 1
        [c] => null 2
        [d] => null 3
        [e] => null 4
        [f] => null 5
        [g] => null 6
        [h] => null 7
        [i] => null 8
        [j] => null 9
        [type] => Multiple Choice
        [location] => 37.334709,-122.034111
    )

   [2] => Array
    (
        [aid] => 432267
        [qid] => 0
        [sid] => 2
        [freetext] => 
        [a] => dog
        [b] => 
        [c] => 
        [d] => 
        [e] => 
        [f] => 
        [g] => 
        [h] => 
        [i] => 
        [j] => 
        [type] => 
        [location] => 
    )

   [3] => Array
    (
        [aid] => 432267
        [qid] => 2
        [sid] => 1
        [freetext] => 
        [a] => dog
        [b] => 
        [c] => 
        [d] => 
        [e] => 
        [f] => 
        [g] => 
        [h] => 
        [i] => 
        [j] => 
        [type] => 
        [location] => 
    )

 )

这是输出(但应该只包含数组[1])

  [0] => Array
    (
        [aid] => 432267
        [qid] => 0
        [sid] => 1
        [freetext] => 
        [a] => Dog
        [b] => null 1
        [c] => null 2
        [d] => null 3
        [e] => null 4
        [f] => null 5
        [g] => null 6
        [h] => null 7
        [i] => null 8
        [j] => null 9
        [type] => Multiple Choice
        [location] => 37.334709,-122.034111
    )

 [1] => Array
    (
        [aid] => 1
        [qid] => 0
        [sid] => 1
        [freetext] => 
        [a] => Dog
        [b] => null 1
        [c] => null 2
        [d] => null 3
        [e] => null 4
        [f] => null 5
        [g] => null 6
        [h] => null 7
        [i] => null 8
        [j] => null 9
        [type] => Multiple Choice
        [location] => 37.334709,-122.034111
    )
   )

但确实如此。我很确定udiff功能不正确,但无法弄明白。我只希望输出sqlite数组中的唯一的

0 个答案:

没有答案