首页 > 解决方案 > 如何使用php比较两个json数据一个是xml另一个是mysql?

问题描述

我确实有两个 json 多维数组,一个来自数据库,另一个来自我的公共路径,它以 xml 形式出现,我如何比较两个数据是否相同?

来自数据库的一个数据,

     $shipment = Shipment::take('10')->get()->toJson();
     $ship =  json_decode($shipment,TRUE);

来自本地存储的其他数据作为 xml 文件,

      $files = File::get(storage_path('xml\Last_FILSDCSEA.xml'));      
      $xml = simplexml_load_string($files, "SimpleXMLElement", LIBXML_NOCDATA);
      $json = json_encode($xml);
      //dd($json);
      $data = json_decode($json,TRUE);

并在下面比较一些东西,

   if(($ship['JS_UniqueConsignRef']) == ($data['JS_UniqueConsignRef']))
    {

        $difference = array_diff_assoc($ship,$data);
        dd($difference);
    }

必须对此代码进行任何更改,请帮忙?

标签: phpmysqljsonxmllaravel

解决方案


我建议您array_diff使用它,通过使用它,您可以确保它们与 JSON 结构中的内容完全匹配。

$ship = json_decode($shipment, TRUE);
$data = json_decode($json, TRUE);
$array_result = array_diff($ship,$data);

if(empty($array_result[0])) {     
    echo "they are same";
}

推荐阅读