PHP:针对特定的JSON数组和正确附加POST数据? [英] PHP: Targeting specific JSON array and appending POST data correctly?

查看:103
本文介绍了PHP:针对特定的JSON数组和正确附加POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,在本质上是结构化的这样一个JSON文件:

I have a JSON file that, in essence, is structured like this:

[{
    "name": "James",
    "reviews": [
        {
            "stars": 5,
            "body": "great!"
        },
        {
            "stars": 1,
            "body": "bad!"
        }
    ]
},
{
    "name": "David",
    "reviews": [
        {
            "stars": 4,
            "body": "pretty good!"
        },
        {
            "stars": 2,
            "body": "just ok..."
        }
    ]
}]

现在并主张大卫新的评价数据时,PHP脚本,我怎么针对大卫的特定的审查,并追加了吗?

Now when positing new review data for David to a PHP script, how do I target David's specific "reviews" and append it?

我已经去codeD一切正常,并有机会获得两个去codeD文件,并发布信息。我只是不知道如何定位的JSON数组在大卫的具体评论...预先感谢您!

I have already decoded everything correctly and have access to both the decoded file and post information. I just don't know how to target David's specific reviews in the JSON array... Thank you in advance!

更新 - 只是要清楚,一切都德$ C $早已CD,POST数据和从服务器的JSON文件。我只需要知道如何定位大卫的评论具体而追加了。

UPDATE - Just to be clear, everything is decoded already, the POST data and the JSON file from the server. I just need to know how to target David's reviews specifically and append it.

更新2 - 每个人,也请您理解,这是该指数是不知道的情况下。这样做[1]将是真棒,但如果有人提出,他们不知道它是什么指标。循环的渲染正在做AngularJS顺便说一句,所以不能在PHP端的前端分配什么。

UPDATE 2 - Everyone, please also understand that this is in the case that the index is not known. Doing [1] would be awesome, but when someone submits, they won't know what index it is. The loop for the rendering is being done in AngularJS btw, so can't assign anything on the PHP side for the front-end.

推荐答案

PHP> = 5.5.0需要 array_column 或参见下面的替代:

PHP >= 5.5.0 needed for array_column or see below for an alternate:

$array[array_search('David', array_column($array, 'name'))]['reviews'][] = array(
    'stars'=>1,'body'=>'meh'
);

而不是 array_column ,你可以使用:

array_map(function($v) { return $v['name']; }, $array);

这篇关于PHP:针对特定的JSON数组和正确附加POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆