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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题