Ahmad
Ahmad

Reputation: 55

How to update an item in a Json object in PHP

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

Upvotes: 2

Views: 20802

Answers (3)

newman
newman

Reputation: 2719

$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 
  }
}

Upvotes: 2

Maug Lee
Maug Lee

Reputation: 915

This works:

$json = '{"fields":[{"name":"news_title","type":"text","value":"old title"},{"name":"news_content","type":"textarea","value":"old content"}]}';
var_dump($json);

$oJson = json_decode($json) ;
$data = 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',
);

foreach ( $data as $key => $val ) {
    foreach ( $oJson->fields as $i => $oVal ) {
        if ( isset($oVal->name) && $oVal->name == $key ) {
            $oJson->fields[$i]->value = $val;
        }
    }
}

$json = json_encode($oJson) ;
var_dump($json);

Upvotes: 2

Yashika Garg
Yashika Garg

Reputation: 2366

You can use an extra counter and start tracking input array and json simultaneously as below:

$count = 0;
foreach(Input::get() as $name => $value){
   echo $name .' : '. $value .' <br /> ' ;
   $source->fields[$count]->name = $name;
   $source->fields[$count]->value = $value;
}

Upvotes: 0

Related Questions