akhil akhil - 3 months ago 8
PHP Question

Compare two json file with empty value

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.

Answer

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.

Comments