foreach数组使用刀片

时间:2018-06-01 20:01:17

标签: php laravel-blade

我开始使用带有codeigniter的Blade模板引擎和Google Api 我使用刀片来开发由另一个开发人员创建的旧项目,因此我必须保持相同的刀片数据结构。

这是由不同的开发人员编写的刀片代码(我需要保留它):

@foreach ($analytics['countries'] as $country)
                            ['{{$country['country']}}',    {{$country['visits_percent']}}],
                    @endforeach

这是我的PHP代码

$this->blade->view()->make('analytics/analytics', ['analytics' => $analytics]);

这是我的$ analtics数组:

 Array
(
    [0] => Array
        (
            [0] => Canada
            [1] => 3367
        )

    [1] => Array
        (
            [0] => United States
            [1] => 202
        )

    [2] => Array
        (
            [0] => Malaysia
            [1] => 34
        )

    [3] => Array
        (
            [0] => Mexico
            [1] => 31
        )

    [4] => Array
        (
            [0] => Peru
            [1] => 23
        )

    [5] => Array
        (
            [0] => Brazil
            [1] => 21
        )

    [6] => Array
        (
            [0] => United Kingdom
            [1] => 21
        )

    [7] => Array
        (
            [0] => Netherlands
            [1] => 17
        )

    [8] => Array
        (
            [0] => Nepal
            [1] => 14
        )

    [9] => Array
        (
            [0] => India
            [1] => 12
        )

    [10] => Array
        (
            [0] => Belarus
            [1] => 11
        )

    [11] => Array
        (
            [0] => France
            [1] => 9
        )

    [12] => Array
        (
            [0] => Ireland
            [1] => 9
        )

    [13] => Array
        (
            [0] => Germany
            [1] => 7
        )

    [14] => Array
        (
            [0] => Philippines
            [1] => 6
        )

    [15] => Array
        (
            [0] => Singapore
            [1] => 5
        )

    [16] => Array
        (
            [0] => Bangladesh
            [1] => 4
        )

    [17] => Array
        (
            [0] => Italy
            [1] => 4
        )

    [18] => Array
        (
            [0] => Serbia
            [1] => 4
        )

    [19] => Array
        (
            [0] => Australia
            [1] => 3
        )

    [20] => Array
        (
            [0] => Belgium
            [1] => 3
        )

    [21] => Array
        (
            [0] => Indonesia
            [1] => 3
        )

    [22] => Array
        (
            [0] => Iran
            [1] => 3
        )

    [23] => Array
        (
            [0] => Jordan
            [1] => 3
        )

    [24] => Array
        (
            [0] => Morocco
            [1] => 3
        )

    [25] => Array
        (
            [0] => China
            [1] => 2
        )

    [26] => Array
        (
            [0] => Colombia
            [1] => 2
        )

    [27] => Array
        (
            [0] => Moldova
            [1] => 2
        )

    [28] => Array
        (
            [0] => Pakistan
            [1] => 2
        )

    [29] => Array
        (
            [0] => Poland
            [1] => 2
        )

    [30] => Array
        (
            [0] => Romania
            [1] => 2
        )

    [31] => Array
        (
            [0] => South Africa
            [1] => 2
        )

    [32] => Array
        (
            [0] => Thailand
            [1] => 2
        )

    [33] => Array
        (
            [0] => Turkey
            [1] => 2
        )

    [34] => Array
        (
            [0] => Argentina
            [1] => 1
        )

    [35] => Array
        (
            [0] => El Salvador
            [1] => 1
        )

    [36] => Array
        (
            [0] => Hong Kong
            [1] => 1
        )

    [37] => Array
        (
            [0] => Japan
            [1] => 1
        )

    [38] => Array
        (
            [0] => Kenya
            [1] => 1
        )

    [39] => Array
        (
            [0] => Latvia
            [1] => 1
        )

    [40] => Array
        (
            [0] => New Zealand
            [1] => 1
        )

    [41] => Array
        (
            [0] => Russia
            [1] => 1
        )

    [42] => Array
        (
            [0] => Saudi Arabia
            [1] => 1
        )

    [43] => Array
        (
            [0] => South Korea
            [1] => 1
        )

)

我的问题是如何为刀片生成相同的数组结构 谢谢

1 个答案:

答案 0 :(得分:1)

我希望这段代码对你有用:

Route::get('/test', function (Request $request) {
$analytics=[
    [
        'Canada',
        3367
    ],
    [
        'United States',
        202
    ],
    [
        'Malaysia',
        34
    ],
];

return view('welcome',compact('analytics'));
})

您可以在view中使用此代码:

@foreach( $analytics as  $analytic)
    <p>
        country = {{$analytic[0]}}
        <br>
        code = {{$analytic[1]}}
        <tr>
    </p>
@endforeach