Saturday, April 1, 2017

Forget Item from an Object Collection while iterating

I currently have an object collection that is being passed to a function that generates HTML form fields. I have a nested foreach loop inside of main foreach loop. I am trying to remove all the items the nested goes through from the collection before the main loop hits it.

It is a bit difficult to explain in text so I have the code I have written below.

As you will see I have other statements in the loop but since it is rather long I have just included what I thought to be the necessary parts.

I am using the Laravel Framework so I am utilizing the collection methods to accomplish this.

Thanks!

<?php
function generateInlineForm($form){
    $form = $form->keyBy('id');
    foreach($form as $key=>$field){
        if($field->type === "label"){?>
            <div class="form-group padding-right-lg align-top">
                <label for="" style="padding-right:10px;"></label>
                <div class="mt-radio-inline">
            <?php $form->forget($key);
            foreach($form->where('name', $field->name)->sortBy('order') as $related_key=>$related_field){
                // $form->forget(840);
                $form->forget($related_key);
                echo($related_key);
                if($related_field->type == "checkbox"){ ?>
                    <label class="mt-checkbox">
                        <input type="checkbox" id="" name="" value=""> 
                        <span></span>
                    </label>
                <?php } else if ($related_field->type == "radio"){ ?>
                <label class="mt-radio">
                    <input type="radio" name="" value=""> 
                    <span></span>
                </label>
        <?php }
            } ?>
                </div>
            </div>
    <?php }



via likwidmonster

Advertisement