如何在laravel中解码Json对象并在laravel中应用foreach循环

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

i am getting this request.   

我收到了这个请求。

 { "area": [
        {
            "area": "kothrud"
        },
        {
            "area": "katraj"
        }
    ]
}

and i want to provide response to this by searching records in database based on above request. how will i decode above json array and use each area field separately.

我想通过根据上述请求在数据库中搜索记录来提供对此的响应。我将如何解码上面的json数组并分别使用每个区域字段。

2 个解决方案

#1


16  

your string is NOT a valid json to start with.

你的字符串不是一个有效的json开头。

a valid json will be,

一个有效的json,

{
    "area": [
        {
            "area": "kothrud"
        },
        {
            "area": "katraj"
        }
    ]
}

if you do a json_decode, it will yield,

如果你做一个json_decode,它会产生,

stdClass Object
(
    [area] => Array
        (
            [0] => stdClass Object
                (
                    [area] => kothrud
                )

            [1] => stdClass Object
                (
                    [area] => katraj
                )

        )

)

Update: to use

更新:使用

$string = '

{
    "area": [
        {
            "area": "kothrud"
        },
        {
            "area": "katraj"
        }
    ]
}

';
            $area = json_decode($string, true);

            foreach($area['area'] as $i => $v)
            {
                echo $v['area'].'<br/>';
            }

Output:

输出:

kothrud
katraj

Update #2:

更新#2:

for that true:

为此:

When TRUE, returned objects will be converted into associative arrays. for more information, click here

如果为TRUE,则返回的对象将转换为关联数组。欲了解更多信息,请点击此处

#2


3  

you can use json_decode function

你可以使用json_decode函数

foreach (json_decode($response) as $area)
{
 print_r($area); // this is your area from json response
}

See this fiddle

看到这个小提琴

#1


16  

your string is NOT a valid json to start with.

你的字符串不是一个有效的json开头。

a valid json will be,

一个有效的json,

{
    "area": [
        {
            "area": "kothrud"
        },
        {
            "area": "katraj"
        }
    ]
}

if you do a json_decode, it will yield,

如果你做一个json_decode,它会产生,

stdClass Object
(
    [area] => Array
        (
            [0] => stdClass Object
                (
                    [area] => kothrud
                )

            [1] => stdClass Object
                (
                    [area] => katraj
                )

        )

)

Update: to use

更新:使用

$string = '

{
    "area": [
        {
            "area": "kothrud"
        },
        {
            "area": "katraj"
        }
    ]
}

';
            $area = json_decode($string, true);

            foreach($area['area'] as $i => $v)
            {
                echo $v['area'].'<br/>';
            }

Output:

输出:

kothrud
katraj

Update #2:

更新#2:

for that true:

为此:

When TRUE, returned objects will be converted into associative arrays. for more information, click here

如果为TRUE,则返回的对象将转换为关联数组。欲了解更多信息,请点击此处

#2


3  

you can use json_decode function

你可以使用json_decode函数

foreach (json_decode($response) as $area)
{
 print_r($area); // this is your area from json response
}

See this fiddle

看到这个小提琴