如何在PHP中的JSON对象,以更新项目 [英] How to update an item in a Json object in PHP

查看:167
本文介绍了如何在PHP中的JSON对象,以更新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象是这样的:

  {
        田:
        [
            {
              名:news_title
              类型:文本,
              值:老题
            },
            {
              名:news_content
              类型:文本区域,
              值:旧的内容
            }
        ]
  }

和我有一个带给我只值这些字段的表单:

 阵列

    [news_title] =>我的新头衔
    [news_content] =>我的新内容
    [other_values​​_doesnt_mach_json_file] =>永远是在数组的末尾

我的目标就是更新JSON对象的新值..所以我得到的JSON对象,象这样的新值:

  {
        田:
        [
            {
              名:news_title
              类型:文本,
              值:我的新称号
            },
            {
              名:news_content
              类型:文本区域,
              值:我的新内容
            }
        ]
  }

在这里,我可以通过JSON对象循环。

  $源= json_de code($ oldjson);的foreach($源 - >领域$场){
        回声现场$>名称。 - 。 $现场>价值。 < BR />';
        // $&现场GT;值='测试';在这里,我试图用新的数据更新每个字段的值..
}

在这里,我怎么能够通过我的新值的数组循环。

  // laravel后
的foreach(输入::得到()为$ NAME => $值){
    回声$名称'。 :。 $价值。 < BR /> ';
}

但我不知道如何将这些丝束结合起来,获得与新值的新JSON对象。

在此先感谢您的帮助。


解决方案

  $源= json_de code($ oldjson);
$输入=输入::得到();的foreach($源 - >字段$ I => $场){
  回声现场$>名称。 - 。 $现场>价值。 < BR />';
  如果(使用isset($输入[$现场>名称])){
    $现场>价值= $输入[$现场>名]。
    // $源[$ i] = $场;不需要这一行,至少我上面的主要问题
  }
}

I have a json object like this :

  {
        "fields" : 
        [
            {
              "name" : "news_title",
              "type" : "text",
              "value" : "old title"
            },
            {
              "name" : "news_content",
              "type" : "textarea",
              "value" : "old content"
            }
        ]            
  }

and I have a form that bring me only the values for these fields :

Array
(
    [news_title] => My new title
    [news_content] => My new content
    [other_values_doesnt_mach_json_file] => always comes at the end of the array
)

My goal is to update that json object with the new values .. so I get the json object with the new values like so :

  {
        "fields" : 
        [
            {
              "name" : "news_title",
              "type" : "text",
              "value" : "My new title"
            },
            {
              "name" : "news_content",
              "type" : "textarea",
              "value" : "My new content"
            }
        ]            
  }

Here I can loop through the Json object ..

$source = json_decode($oldjson);

foreach($source->fields as $field){
        echo $field->name .' - '. $field->value .' <br />';
        //$field->value =  'test'; Here I'm trying to update the values of each field with the new data ..
}

And here how I can loop through my new values array ..

    // laravel post 
foreach(Input::get() as $name => $value){
    echo $name .' : '. $value .' <br /> ' ;
}

But I have no idea how to combine those tow to get a new json object with the new values.

Thanks in advance for your help

解决方案

$source = json_decode($oldjson);
$input = Input::get();

foreach($source->fields as $i => $field) {
  echo $field->name .' - '. $field->value .' <br />';
  if (isset($input[$field->name])) {
    $field->value = $input[$field->name];
    //$source[$i] = $field; no need for this line, at least for my main issue above 
  }
}

这篇关于如何在PHP中的JSON对象,以更新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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