user6250770
user6250770

Reputation: 690

replace values of keys in json1 from Json2

I am very very new to php.. actually i am from java domain. But, i have to do some work in php for integration. My scenario is, i have one json array which will have 4 keys for ex:

 one json --> {"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}.

I will be getting another JSON which ever edited from admin panel. for example if i updated any key, only that key will coming in the

 second JSON --> for ex: {"blog_heading":"def"}

Now, i have to replace the value of second json to first json. example output for above scenario like I am very very new to php.. actually i am from java domain. But, i have to do some work in php for integration. My scenario is, i have one json array which will have 4 keys for ex:

  output json --> {"id":7,"active":1,"blogId":"abc","blog_heading":"def"}.

So i am trying as below,

$id = json_decode($data_string);
$id2 = json_encode($post);
$id5 = json_decode($id2);
$id6 = array();
foreach ($id as $key => $value) 
{

    $log->debug($key . ': ' . $value);
    if (array_key_exists($key, $id5->data)) {
        $log->debug($key . 'element is in the array');
        $log->debug($value . 'element is in the array');
        //array_push($id5, "apple", "raspberry");

        $id3 = array($key  => $value);
        $id3[$key] = $value;
        $log->debug($id3);

    }else{
        $log->debug($key . 'element is not in the array');
    }

}
$id7 = json_encode($id2);
$log->debug($id7);

id5 data is : $id5

  DEBUG - 2017-06-05T02:26:20-04:00 - stdClass Object
   (
[meta] => stdClass Object
    (
        [table] => story
        [type] => item
    )

[data] => stdClass Object
    (
        [id] => 7
        [active] => 1
        [blogId] => abc
        [blog_heading] => xyz

    )

    )


 ==================

Log of $id :

stdClass Object
   (
[active] => 1
[blog_heading] => def
[id] => 7
 )

Please suggest me how can i achieve this... Anything i am doing wrong here

Upvotes: 0

Views: 67

Answers (3)

Nidhi
Nidhi

Reputation: 1539

If you have only one element in array,Do like this

$a = json_decode('{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}',true);
$b = json_decode('{"blog_heading":"def"}',true);
$a['blog_heading'] = $b['blog_heading'];   
print_r($a);

If you have multiple element like this :

$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"blog_heading":"def"},{"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
    $c[$i]['blog_heading'] = $d[$i]['blog_heading'];
    $return[] = $c[$i];
}
print_r($return);

If you want to replace value by specific id

$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"id":7,"blog_heading":"def"},{"id":9,"blog_heading":"hello"}]',true);
$return = array();

for ($i=0; $i < count($c); $i++) {
    if($d[$i]['id'] == $c[$i]['id']) {
        $c[$i]['blog_heading'] = $d[$i]['blog_heading'];
    }
    $return[] = $c[$i];
}
print_r($return);

Checking dynamic key value pair :

$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"id":6,"blog_heading":"def"},{"id":9,"blog_heading":"hello"}]',true);

$return = array();
for ($i=0; $i < count($c); $i++) {
    $result = array_intersect_key($c[$i], $d[$i]);
    foreach ($result as $key => $value) {
        $c[$i][$key] = $d[$i][$key];
    }
    $return[] = $c[$i];
}
print_r($return);

Check demo here

Upvotes: 0

Piyush Kumar
Piyush Kumar

Reputation: 48

<?php
$json1='{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}';
$json2='{"blog_heading":"def"}';

$json1=json_decode($json1);
$json2=json_decode($json2);
foreach ($json1 as $key => $value) {
    if($json2->$key){
        $json1->$key=$json2->$key;
    }
}

$json1=json_encode($json1);
$json2=json_encode($json2);

Upvotes: 0

VirCom
VirCom

Reputation: 3632

Please try that:

$j1 = '{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}';
$j2 = '{"blog_heading":"def"}';

$result = json_encode(
    array_merge(
        json_decode($j1, true),
        json_decode($j2, true)
   )
);

Upvotes: 1

Related Questions