根据顺序比较两个数组

时间:2017-07-28 12:23:30

标签: php laravel

如何根据ex的顺序比较两个数组。如果数组1是{1,2,3},则数组2是{1,2,3},则显示为true。数组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";
                                      } 
                                }

              }

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

$arr1Collection1 = implode(', ', $array1);
$arr2Collection2 = implode(', ', $array2);

if($arr1Collection1 == $arr2Collection2){
echo true;
} else{
echo false;
}

答案 3 :(得分:0)

  $test1 = [1,2,3];
  $test2 = [1,3,2];

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