0

How can I compare two arrays depending on the order for ex. if array 1 is {1, 2 , 3} and array 2 is {1,2,3} display true else for ex. array 2 {1,3,2} display false.. this is my code so far..

    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";
                                      } 
                                }

              }
ylli
  • 53
  • 2
  • 9

4 Answers4

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

use diff available in collection

$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
Arun pandian M
  • 862
  • 10
  • 17
0
$arr1Collection1 = implode(', ', $array1);
$arr2Collection2 = implode(', ', $array2);

if($arr1Collection1 == $arr2Collection2){
echo true;
} else{
echo false;
}
safin chacko
  • 1,345
  • 1
  • 11
  • 18
0
  $test1 = [1,2,3];
  $test2 = [1,3,2];

  if ($test1 === $test2) {
      echo "true";
  } else {
      echo "false";
  }
Lesiuk Alexey
  • 237
  • 1
  • 2
  • 7