2017-07-28 125 views
0

如何根据前序的顺序比较两个阵列?如果数组1是{1,2,3}并且数组2是{1,2,3},则显示其他前缀。阵列2 {1,3,2}显示错误..这是到目前为止我的代码..根据顺序比较两个阵列

foreach($questions as $question){ 

        $question_answers = OrderingAnswer::where('question_id', $question->id) 
            ->where('deleted',0) 
            ->get() 
            ->toArray(); 


        $question_answer = $request->except('_token', 'test_id'); 

        $answers = $question_answer[ $question->id]; 



          foreach($question_answers as $answer){ 



           if($answers === $question_answers){ 


            echo "true"; 
             } 
             else{ 
            echo "false"; 
             } 
           } 

       } 
+0

你的意思是你想基于键或索引进行比较吗? –

+0

你可以看看 - > https://stackoverflow.com/questions/5678959/php-check-if-two-arrays-are-equal – Maris

+0

我有“id”=> 239,我有239这样的顺序, 240和我想要比较 – ylli

回答

0
you can refer this example: 
<select name="tags" multiple required> 
@foreach ($tags as $name) 
@foreach($item->tags as $itemtag) 
    @if($name == $itemtag->name) 
     <option value="{{$name}}" selected>{{$name}}</option> 
     <?php continue 2; ?> 
    @endif 
@endforeach 
<option value="{{$name}}">{{$name}}</option> 
@endforeach 
0

使用diff提供收集

$collection = collect([1, 2, 3, 4, 5]); 

$diff = $collection->diff([2, 4, 6, 8]); 

$diff->all(); //it will return the non-common values present in the first array [1, 3, 5] 

$diff->isEmpty(); // it will return true if both array are common 
$diff->isNotEmpty(); // returns false if both array are common 
0
$arr1Collection1 = implode(', ', $array1); 
$arr2Collection2 = implode(', ', $array2); 

if($arr1Collection1 == $arr2Collection2){ 
echo true; 
} else{ 
echo false; 
} 
0
$test1 = [1,2,3]; 
    $test2 = [1,3,2]; 

    if ($test1 === $test2) { 
     echo "true"; 
    } else { 
     echo "false"; 
    }