dotif64826 2016-08-16 06:50
浏览 55
已采纳

将两个json文件与空值进行比较

I have two json file, and need to sync them, if changes found, below is my json file,

enter image description here

is parent(P) and another one

enter image description here

is child(C),

Here is my code :

        foreach ($localJson->PushNotification as $key => $data ) {

            foreach ($this->news as $news ) {
                if($news->pushMessageId == $data->pushMessageId){

                    if($news->Image != "" && $news->Image != $data->Image){
                        echo "new image found <br />";
                    }  
                    if($news->documentFile != "" && $news->documentFile != $data->documentFile){
                        echo "new documentFile found <br />";
                    }  
                    if($news->categoryIcon != "" && $news->categoryIcon != $data->categoryIcon){
                        echo "new categoryIcon found <br />";
                    }  
                }
            }
        }

The Problem is everytime when it loops, it doesent discard previous loops value, if current found to be empty

For Ex: 1st loop 1653 pushMessageId compares pdf and updates in child, but for next pushMessageId again it takes same pdf to compare, rather then taking empty field.

  • 写回答

1条回答 默认 最新

  • du9843 2016-08-16 09:38
    关注

    I have check your code , its look a simple logical condition and need to update current objects new value.

        foreach ($localJson->PushNotification as $key => $data ) {
    
            foreach ($this->news as $news ) {
                if($news->pushMessageId == $data->pushMessageId){
    
                    if($news->Image != "" && $news->Image != $data->Image){
                        $data->Image = $news->Image;
                    }  
                    if($news->documentFile != "" && $news->documentFile != $data->documentFile){
                        $data->documentFile = $news->documentFile;
                    }  
                    if($news->categoryIcon != "" && $news->categoryIcon != $data->categoryIcon){
                        $data->categoryIcon = $news->categoryIcon;
                    }  
                }
            }
        }
    

    I hope this solution may work for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?