php - Best way to compare key and value of array(s) in Laravel -
currently have 2 arrays shown in picture below. best way compare them? either combining them , compare within 1 array or compare way did?
$array1
$array2
this did compare them
<table> <thead><tr><td>status</td></tr></thead> <tbody> <tr> <td> foreach($array1 $key => $value) { foreach($array2 $ke2 => $value2) { if($value[0] == $value2[0] && $value[1] == $value2[1] && $value[2] == $value2[2]) yes else no } } </td> <tr> </tbody> </table> updated
<table> <thead><tr><td>status</td></tr></thead> <tbody> <tr> <td> @foreach ($array1 $key => $value) @if (isset($array2[$key]) && $value == $array2[$key]) yes @else no @endif @endforeach </td> <tr> </tbody> </table> but display in table this
status
noyesyes
noyesyes
noyesyes
suppose be
status
no
yes
yes
equivalency works arrays, can eliminate inner foreach loop
foreach ($array1 $key => $value) { echo isset($array2[$key]) && $value == $array2[$key] ? 'yes' : 'no'; } you can use === typesafe comparisons , order of keys important. see also: compare multidimensional arrays in php
if you're looking how output template, blade has own syntax loops , conditionals.
@foreach ($array1 $key => $value) @if (isset($array2[$key]) && $value == $array2[$key]) yes @else no @endif @endforeach check documentation page more on blade templating syntax: https://laravel.com/docs/5.4/blade#loops


Comments
Post a Comment