dongxiaoguang9108 2017-06-05 05:49
浏览 55

从Json2替换json1中的键值

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

  • 写回答

3条回答 默认 最新

  • douyiyi5284 2017-06-05 06:09
    关注

    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)
       )
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?