T
T
the5x2019-03-22 08:27:00
Laravel
the5x, 2019-03-22 08:27:00

Two associative arrays of different lengths, how to compare values ​​and apply to checkboxes?

There are two associative arrays of different lengths, how to compare their values ​​and apply them to checkboxes? So that if the values ​​match, that checkbox gets the attribute checked. Now I get it like this:

  • Politics
  • Video (checked)
  • Politics (checked)
  • Video

And I want
  • Video (checked)
  • Politics (checked)

Here is the code, but in it, I'm doing something wrong, can you help
@foreach($category as $categories)
                <input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}

                @foreach($post->category as $selected)
                    @if($selected->title == $categories->title)
                    <input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
                    @endif
                @endforeach


            @endforeach

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Savrasov, 2019-03-22
@alcorn

Hmmm, if I understood the problem correctly, then

@foreach($category as $categories)
                @foreach($post->category as $selected)
                    @if($selected->title == $categories->title)
                    <input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
                    @endif
                @endforeach


            @endforeach

N
NubasLol, 2019-03-22
@NubasLol

php.net/manual/en/function.array-diff.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question