如何在Laravel中的一个foreach循环中打印两个数组

时间:2017-07-22 18:56:01

标签: php arrays foreach laravel-blade

我在这个forech循环中打印这两个数组。如果我在提交后选择最后一个复选框项目,它将检查第一个项目没有检查最后一项。这是我的问题。

这是我的代码示例图片 Here is my code example image

问题 problem

我也尝试其他问题的答案,但对我不起作用。

  1. How to use multiple arrays in " single " foreach() loop

  2. Two arrays in foreach loop

  3. How to display two arrays in one foreach loop?

  4. 提前致谢。

3 个答案:

答案 0 :(得分:1)

我使用两个php数组函数解决了我的问题。

  1. array_column
  2. in_array
  3. 以下是我解决的代码:

        @elseif(isset($groupsData) && !$groupsData->isEmpty() or isset($qids) && !$qids->isEmpty())
        <?php
            $i = 0;
            $gids = array_column($qids->toArray(),'group_id');
        ?>
        @foreach($groupsData as $key => $data)
            <?php $i++;  ?>
            <tr>
                <td><label>
                <input type="checkbox" name="groups_id[]" value="{{ $data->id }}" class="check_checkbox" @if(in_array($data->id,$gids)) {{ 'checked' }} @endif >
                </label></td>
                <td>{{ $i }}</td>
                <td>{{ $data->group_name }}</td>
            </tr>
        @endforeach
    @else
    

    <强>截图:

    enter image description here

答案 1 :(得分:0)

我的刀片代码:

@elseif(isset($groupsData) && !$groupsData->isEmpty() or isset($qids) && !$qids->isEmpty())
<?php $i = 0; ?>
@foreach($groupsData as $key => $data)
    <?php $i++; ?>
    <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="{{ $data->id }}" class="check_checkbox" @if(isset($qids[$key]->group_id)) {{ 'checked' }} @endif ></label></td>
        <td>{{ $i }}</td>
        <td>{{ $data->group_name }}</td>
    </tr>
@endforeach @else

这是print_r代码:

Illuminate\Database\Eloquent\Collection Object

(     [items:protected] =&gt;排列         (             [0] =&gt; App \ QuizGroupQuestionId对象                 (                     [fillable:protected] =&gt;排列                         (                             [0] =&gt; quiz_id                             [1] =&gt; GROUP_ID                             [2] =&gt; question_id                         )

                [dates:protected] => Array
                    (
                        [0] => deleted_at
                    )

                [connection:protected] => mysql
                [table:protected] => 
                [primaryKey:protected] => id
                [keyType:protected] => int
                [incrementing] => 1
                [with:protected] => Array
                    (
                    )

                [withCount:protected] => Array
                    (
                    )

                [perPage:protected] => 15
                [exists] => 1
                [wasRecentlyCreated] => 
                [attributes:protected] => Array
                    (
                        [id] => 12
                        [quiz_id] => 1
                        [group_id] => 8
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 12
                        [quiz_id] => 1
                        [group_id] => 8
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [casts:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [appends:protected] => Array
                    (
                    )

                [events:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [relations:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [timestamps] => 1
                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [forceDeleting:protected] => 
            )

        [1] => App\QuizGroupQuestionId Object
            (
                [fillable:protected] => Array
                    (
                        [0] => quiz_id
                        [1] => group_id
                        [2] => question_id
                    )

                [dates:protected] => Array
                    (
                        [0] => deleted_at
                    )

                [connection:protected] => mysql
                [table:protected] => 
                [primaryKey:protected] => id
                [keyType:protected] => int
                [incrementing] => 1
                [with:protected] => Array
                    (
                    )

                [withCount:protected] => Array
                    (
                    )

                [perPage:protected] => 15
                [exists] => 1
                [wasRecentlyCreated] => 
                [attributes:protected] => Array
                    (
                        [id] => 13
                        [quiz_id] => 1
                        [group_id] => 9
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 13
                        [quiz_id] => 1
                        [group_id] => 9
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [casts:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [appends:protected] => Array
                    (
                    )

                [events:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [relations:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [timestamps] => 1
                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [forceDeleting:protected] => 
            )

        [2] => App\QuizGroupQuestionId Object
            (
                [fillable:protected] => Array
                    (
                        [0] => quiz_id
                        [1] => group_id
                        [2] => question_id
                    )

                [dates:protected] => Array
                    (
                        [0] => deleted_at
                    )

                [connection:protected] => mysql
                [table:protected] => 
                [primaryKey:protected] => id
                [keyType:protected] => int
                [incrementing] => 1
                [with:protected] => Array
                    (
                    )

                [withCount:protected] => Array
                    (
                    )

                [perPage:protected] => 15
                [exists] => 1
                [wasRecentlyCreated] => 
                [attributes:protected] => Array
                    (
                        [id] => 14
                        [quiz_id] => 1
                        [group_id] => 10
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 14
                        [quiz_id] => 1
                        [group_id] => 10
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [casts:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [appends:protected] => Array
                    (
                    )

                [events:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [relations:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [timestamps] => 1
                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [forceDeleting:protected] => 
            )

    )

呈现的HTML代码:

    <table class="table table-striped table-bordered table-hover table-responsive">
<thead>
<tr>
<th><label><input name="group_all" class="checked_all" type="checkbox"> Select All</label></th>
<th>#</th>
<th>Group Name</th>
</tr>
</thead>
<tbody> 
    <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="4" class="check_checkbox"  checked  ></label></td>
        <td>1</td>
        <td>jkl</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="5" class="check_checkbox"  checked  ></label></td>
        <td>2</td>
        <td>mno</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="7" class="check_checkbox"  checked  ></label></td>
        <td>3</td>
        <td>rthytr4y</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="8" class="check_checkbox"  ></label></td>
        <td>4</td>
        <td>erter</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="9" class="check_checkbox"  ></label></td>
        <td>5</td>
        <td>wetret</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="10" class="check_checkbox"  ></label></td>
        <td>6</td>
        <td>yhjjyukji</td>
    </tr>
    </tbody>
</table>

答案 2 :(得分:0)

好的,您可以看到所有单选按钮都已选中。你需要更好的检查。使用此检查,请根据刀片语法修复语法。但基本上你要将$ qids中的group_id与$ data中的id进行比较。

@if(isset($qids[$key]->group_id) && $qids[$key]->group_id == $data->$id ) {{ 'checked' }} @endif