1

Currently I have two arrays as shown in the picture below. What is the best way to compare them? Either by combining them together and compare within one array or compare the way I did?

$array1

$array1

$array2

enter image description here

This is what i did to compare them

<table>
<thead><tr><td>status</td></tr></thead>

<tbody>
    <tr>
        <td>
            foreach($array1 as $key => $value)
            {
                foreach($array2 as $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 as $key => $value)
              @if (isset($array2[$key]) && $value == $array2[$key])
                Yes
              @else
                No
              @endif
            @endforeach
        </td>
    <tr>
</tbody>
</table>

but this display in the table like this

Status

NoYesYes

NoYesYes

NoYesYes

Suppose to be

Status

No

Yes

Yes

halfer
  • 19,824
  • 17
  • 99
  • 186
begineeeerrrr
  • 323
  • 2
  • 7
  • 17

2 Answers2

1

Equivalency works for arrays, so you can eliminate the inner foreach loop

foreach ($array1 as $key => $value) {
    echo isset($array2[$key]) && $value == $array2[$key] ? 'YES' : 'NO';
}

You can also use === for typesafe comparisons and where the order of keys is important. See also: Compare multidimensional arrays in PHP

If you're looking for how to output into a template, Blade has its own syntax for loops and conditionals.

@foreach ($array1 as $key => $value)
    @if (isset($array2[$key]) && $value == $array2[$key])
        Yes
    @else
        No
    @endif
@endforeach

Check the documentation page for more on Blade templating syntax: https://laravel.com/docs/5.4/blade#loops

Elle H
  • 11,837
  • 7
  • 39
  • 42
  • how do i put this in laravel blade view? it just display the codes – begineeeerrrr Aug 15 '17 at 17:36
  • I expanded my answer to include information about blade. Though I'll also note blade templates also work like any other PHP file and you can add blocks wherever you want, though it's recommended to avoid pulling in business logic into your templates. – Elle H Aug 15 '17 at 17:42
  • Also if you want this in a table, do you want them as cells (left to right blocks in the table) or a single cell, one per row (stacked top to bottom)? You'll need to add the appropriate cell (``) and row (``) tags to format it as you desire. – Elle H Aug 15 '17 at 17:44
  • You just need to add markup for the row. You've placed the loop in the middle of a cell, e.g. `
    loop-is-here-right-now
    `. If you want the output to be a row with a single cell, the loop should be where the row goes, e.g. `loop-should-go-here
    `. And the loop should output the markup for the row, e.g. `Yes`
    – Elle H Aug 15 '17 at 18:00
  • hmmm...any ideas how to do that? i can think by assigning a new array and inside the new array is the answer yes/no – begineeeerrrr Aug 15 '17 at 18:15
  • Just move `` and `` inside the foreach loop. ` @foreach (...) @endforeach
    @if (...) ... @endif
    `
    – Elle H Aug 15 '17 at 18:26
1

To get this working in a laravel view you could use the for loop.

@foreach ($array1 as $key => $value)
    {{isset($array2[$key]) && $value == $array2[$key] ? 'YES' : 'NO'}}
@endforeach

To explain:

The inner part is a ternary operator, it evaluates the equality and return the respective value.

When we have the

milo526
  • 5,012
  • 5
  • 41
  • 60