在PHP laravel中循环遍历JSON数组

时间:2022-10-26 09:16:21

Controller:

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class welcomeController extends Controller
{
    public function welcome() {
        $cards = json_decode('[{
            "id": 1,
            "name ": "a",
            "class": "class1"
        }, {
            "id": 2,
            "name ": "b",
            "class": "class2"
        }]');

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

welcome.blade.php

welcome.blade.php

@foreach($cards as $card)
    <p>{{ $card->name }} </p>
    <p>{{ $card->class }} </p>
@endforeach

Error message on browser 在PHP laravel中循环遍历JSON数组

浏览器上的错误消息

I'm new to PHP laravel. Please help me to find proper solution for this.Thanks.

我是PHP laravel的新手。请帮我找到合适的解决方案。谢谢。

4 个解决方案

#1


1  

Hello you have everything ok in your code The issue is in the json variable $cards = json_decode('[{ "id": 1, "name ": "a", "class": "class1" }, { "id": 2, "name ": "b", "class": "class2" }]');

您好,代码中的一切正常问题在于json变量$ cards = json_decode('[{“id”:1,“name”:“a”,“class”:“class1”},{“id” :2,“name”:“b”,“class”:“class2”}]');

the key "name " has some space inside so in view you are tring to access as "name" that's why the error is coming

键“名称”内部有一些空间,所以在视图中你要以“名称”的形式访问,这就是错误即将到来的原因

#2


1  

try with

试试吧

 return view('welcome')->with("cards",$cards);

#3


0  

From your screenshot, the 'cards' value being sent to the template is an array of arrays rather than an array of objects that your code snippet would produce.

从屏幕截图中,发送到模板的“卡片”值是一个数组数组,而不是代码片段会产生的对象数组。

The second argument to json_decode() is whether to convert to an associative array or not, defaulting to not.

json_decode()的第二个参数是否转换为关联数组,默认为不。

#4


0  

The error is happening because your json array is multidimensional, and your welcome.blade.php is calling the variables of name and class which don't exist at that level of the array.

发生错误是因为你的json数组是多维的,而welcome.blade.php正在调用name和class的变量,这些变量在数组的那个级别不存在。

In your welcome.blade.php, change your foreach to say:

在welcome.blade.php中,改变你的foreach说:

@foreach($cards[0] as $card)
    <p>{{ $card->name }} </p>
    <p>{{ $card->class }} </p>
@endforeach

I believe that should get you back on track. Hope that helps!

我相信这会让你回到正轨。希望有所帮助!

#1


1  

Hello you have everything ok in your code The issue is in the json variable $cards = json_decode('[{ "id": 1, "name ": "a", "class": "class1" }, { "id": 2, "name ": "b", "class": "class2" }]');

您好,代码中的一切正常问题在于json变量$ cards = json_decode('[{“id”:1,“name”:“a”,“class”:“class1”},{“id” :2,“name”:“b”,“class”:“class2”}]');

the key "name " has some space inside so in view you are tring to access as "name" that's why the error is coming

键“名称”内部有一些空间,所以在视图中你要以“名称”的形式访问,这就是错误即将到来的原因

#2


1  

try with

试试吧

 return view('welcome')->with("cards",$cards);

#3


0  

From your screenshot, the 'cards' value being sent to the template is an array of arrays rather than an array of objects that your code snippet would produce.

从屏幕截图中,发送到模板的“卡片”值是一个数组数组,而不是代码片段会产生的对象数组。

The second argument to json_decode() is whether to convert to an associative array or not, defaulting to not.

json_decode()的第二个参数是否转换为关联数组,默认为不。

#4


0  

The error is happening because your json array is multidimensional, and your welcome.blade.php is calling the variables of name and class which don't exist at that level of the array.

发生错误是因为你的json数组是多维的,而welcome.blade.php正在调用name和class的变量,这些变量在数组的那个级别不存在。

In your welcome.blade.php, change your foreach to say:

在welcome.blade.php中,改变你的foreach说:

@foreach($cards[0] as $card)
    <p>{{ $card->name }} </p>
    <p>{{ $card->class }} </p>
@endforeach

I believe that should get you back on track. Hope that helps!

我相信这会让你回到正轨。希望有所帮助!