2017-07-28 417 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"; 
    }