I have got two associative arrays of different lengths. I want to know how to compare values and apply to checkboxes?How to sort an array of associative arrays by value of a given key in PHP?Codeigniter - Form validation converting post variables from Array to “Array” stringPHP foreach loop (output into CSS)Getting both or one Checkbox value in for each loophow to insert multiple values of array checkbox in one row with different field nameHow to assign values from associative array to check boxes using foreach in smarty?Is there any way to get ONLY the selects inputs from a form?Show checked=“checked” outside foreachbootstrap how to add/remove category just like opencart?Generate HTML to show custom questions with the correct type (text, checkbox, …) and add the required attribute properly

bldc motor, esc and battery draw, nominal vs peak

Who was the lone kid in the line of people at the lake at the end of Avengers: Endgame?

What term is being referred to with "reflected-sound-of-underground-spirits"?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Can SQL Server create collisions in system generated constraint names?

What are the steps to solving this definite integral?

Critique of timeline aesthetic

Mistake in years of experience in resume?

Relationship between strut and baselineskip

How to prevent z-fighting in OpenSCAD?

Why does nature favour the Laplacian?

Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?

Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?

Dynamic SOQL query relationship with field visibility for Users

What does ゆーか mean?

How come there are so many candidates for the 2020 Democratic party presidential nomination?

Was there a Viking Exchange as well as a Columbian one?

How could Tony Stark make this in Endgame?

What's the polite way to say "I need to urinate"?

How would 10 generations of living underground change the human body?

Aligning equation numbers vertically

Don’t seats that recline flat defeat the purpose of having seatbelts?

Check if a string is entirely made of the same substring

Is there any official lore on the Far Realm?



I have got two associative arrays of different lengths. I want to know how to compare values and apply to checkboxes?


How to sort an array of associative arrays by value of a given key in PHP?Codeigniter - Form validation converting post variables from Array to “Array” stringPHP foreach loop (output into CSS)Getting both or one Checkbox value in for each loophow to insert multiple values of array checkbox in one row with different field nameHow to assign values from associative array to check boxes using foreach in smarty?Is there any way to get ONLY the selects inputs from a form?Show checked=“checked” outside foreachbootstrap how to add/remove category just like opencart?Generate HTML to show custom questions with the correct type (text, checkbox, …) and add the required attribute properly






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.



Now I have this:



  • Politics

  • Video (Checked)

  • Policy (Checked)

  • Video

I want



  • Video (Checked)

  • Policy (Checked)

My code at the monent:



@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









share|improve this question
























  • You do have the result with you. No? Or do you want only checked boxes to show up?

    – vivek_23
    Mar 22 at 5:38

















0















There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.



Now I have this:



  • Politics

  • Video (Checked)

  • Policy (Checked)

  • Video

I want



  • Video (Checked)

  • Policy (Checked)

My code at the monent:



@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









share|improve this question
























  • You do have the result with you. No? Or do you want only checked boxes to show up?

    – vivek_23
    Mar 22 at 5:38













0












0








0








There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.



Now I have this:



  • Politics

  • Video (Checked)

  • Policy (Checked)

  • Video

I want



  • Video (Checked)

  • Policy (Checked)

My code at the monent:



@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









share|improve this question
















There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.



Now I have this:



  • Politics

  • Video (Checked)

  • Policy (Checked)

  • Video

I want



  • Video (Checked)

  • Policy (Checked)

My code at the monent:



@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






php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 5:45









Rahul Meshram

9,16442243




9,16442243










asked Mar 22 at 5:36









Alex SmirnovAlex Smirnov

14




14












  • You do have the result with you. No? Or do you want only checked boxes to show up?

    – vivek_23
    Mar 22 at 5:38

















  • You do have the result with you. No? Or do you want only checked boxes to show up?

    – vivek_23
    Mar 22 at 5:38
















You do have the result with you. No? Or do you want only checked boxes to show up?

– vivek_23
Mar 22 at 5:38





You do have the result with you. No? Or do you want only checked boxes to show up?

– vivek_23
Mar 22 at 5:38












2 Answers
2






active

oldest

votes


















0














You can apply that condition inside element to apply checked property



@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)
<input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" $selected->title == $categories->title ? 'checked' : ''> $categories->title
@endforeach
@endforeach





share|improve this answer






























    0














    I have resolved this problem



    Controller:



     $checkedCategory = [];

    foreach($post->category as $selected)
    for($i = 0; $i < count($category); $i++)
    if($selected->title == $category[$i]->title)
    $checkedCategory[$i] = $category[$i]->title;




    $checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));


    Template:



    <fieldset>
    <legend>Категории</legend>

    @foreach($category as $categories)
    @if(in_array($categories->title, $checkedCategory))
    <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" checked> $categories->title
    @else
    <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]"> $categories->title
    @endif
    @endforeach

    </fieldset>





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293474%2fi-have-got-two-associative-arrays-of-different-lengths-i-want-to-know-how-to-co%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You can apply that condition inside element to apply checked property



      @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)
      <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" $selected->title == $categories->title ? 'checked' : ''> $categories->title
      @endforeach
      @endforeach





      share|improve this answer



























        0














        You can apply that condition inside element to apply checked property



        @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)
        <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" $selected->title == $categories->title ? 'checked' : ''> $categories->title
        @endforeach
        @endforeach





        share|improve this answer

























          0












          0








          0







          You can apply that condition inside element to apply checked property



          @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)
          <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" $selected->title == $categories->title ? 'checked' : ''> $categories->title
          @endforeach
          @endforeach





          share|improve this answer













          You can apply that condition inside element to apply checked property



          @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)
          <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" $selected->title == $categories->title ? 'checked' : ''> $categories->title
          @endforeach
          @endforeach






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 5:42









          Rahul MeshramRahul Meshram

          9,16442243




          9,16442243























              0














              I have resolved this problem



              Controller:



               $checkedCategory = [];

              foreach($post->category as $selected)
              for($i = 0; $i < count($category); $i++)
              if($selected->title == $category[$i]->title)
              $checkedCategory[$i] = $category[$i]->title;




              $checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));


              Template:



              <fieldset>
              <legend>Категории</legend>

              @foreach($category as $categories)
              @if(in_array($categories->title, $checkedCategory))
              <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" checked> $categories->title
              @else
              <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]"> $categories->title
              @endif
              @endforeach

              </fieldset>





              share|improve this answer



























                0














                I have resolved this problem



                Controller:



                 $checkedCategory = [];

                foreach($post->category as $selected)
                for($i = 0; $i < count($category); $i++)
                if($selected->title == $category[$i]->title)
                $checkedCategory[$i] = $category[$i]->title;




                $checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));


                Template:



                <fieldset>
                <legend>Категории</legend>

                @foreach($category as $categories)
                @if(in_array($categories->title, $checkedCategory))
                <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" checked> $categories->title
                @else
                <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]"> $categories->title
                @endif
                @endforeach

                </fieldset>





                share|improve this answer

























                  0












                  0








                  0







                  I have resolved this problem



                  Controller:



                   $checkedCategory = [];

                  foreach($post->category as $selected)
                  for($i = 0; $i < count($category); $i++)
                  if($selected->title == $category[$i]->title)
                  $checkedCategory[$i] = $category[$i]->title;




                  $checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));


                  Template:



                  <fieldset>
                  <legend>Категории</legend>

                  @foreach($category as $categories)
                  @if(in_array($categories->title, $checkedCategory))
                  <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" checked> $categories->title
                  @else
                  <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]"> $categories->title
                  @endif
                  @endforeach

                  </fieldset>





                  share|improve this answer













                  I have resolved this problem



                  Controller:



                   $checkedCategory = [];

                  foreach($post->category as $selected)
                  for($i = 0; $i < count($category); $i++)
                  if($selected->title == $category[$i]->title)
                  $checkedCategory[$i] = $category[$i]->title;




                  $checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));


                  Template:



                  <fieldset>
                  <legend>Категории</legend>

                  @foreach($category as $categories)
                  @if(in_array($categories->title, $checkedCategory))
                  <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]" checked> $categories->title
                  @else
                  <input type="checkbox" aria-label="$categories->title" value="$categories->title" name="category[$categories->id]"> $categories->title
                  @endif
                  @endforeach

                  </fieldset>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 17:23









                  Alex SmirnovAlex Smirnov

                  14




                  14



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293474%2fi-have-got-two-associative-arrays-of-different-lengths-i-want-to-know-how-to-co%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript