i am getting this request.   

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

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

推荐答案

字符串开头不是有效的json.

a valid json will be,

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

if you do a json_decode, it will yield,

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:

for that true:

如果为TRUE,则返回的对象将转换为关联array.欲了解更多信息,请点击click here

Laravel相关问答推荐

在Laravel Lumen中迭代连接查询数据

Uncaught ReferenceError:未定义require[Shopify PHP React Browser问题]

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

如何使用PHP版本8.2.6安装beyondcode/laravel-websockets软件包?

前端和管理员分开时未加载laravel 9中间件

(1/1) ErrorException ReflectionFunction::__construct() ex

函数接受 Laravel 集合对象,尽管只允许使用字符串

Laravel Eloquent 查找日期超过 2 天的行

在 laravel 4.2 中,用户 'root'@'localhost' 的 Laravel 访问被拒绝(使用密码:YES)

所有 POST 请求上的 Laravel 4 CSRF

Laravel 5.3 创建模型返回字段没有默认值

laravel url 验证变音符号

laravel:如何在 Eloquent Query 中使用 LOWERCASE 函数?

Laravel 项目旁边的 Wordpress 项目(在 public_html 文件夹中)

Laravel Eloquent 模型中的字段

在 Laravel 中包含 bootstrap 程序

今天之前的 Laravel 规则......怎么办

Laravel 5.4 Vue.JS 无法挂载组件:未定义模板或渲染函数

如何在 Laravel 5 中验证 RESTful API?

你如何在 Laravel 5.3 的新通知服务生成的Electron邮件中添加图像?