php - 如何使用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);
}
必须对此代码进行任何更改,请帮忙?
解决方案
我建议您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";
}
推荐阅读
- python - OSError: [Errno 1] 不允许操作
- go - Go中使用递归和并发的第N个斐波那契数
- python - 在使用 keras Lambda 时,如何处理“TypeError: Object arrays are not supported”?
- python - Python 给了我索引号而不是列表中的输入。
- swift - 敏感数据的 Vapor 3 GET 路由
- rest - 为 NFL api 生成访问令牌
- java - Java,获取与输入部分匹配的模式部分
- java - servlet 中的 Beanutils 有问题
- r - 变异替换,如何指定为字符
- uwp - 似乎 UWP Async IO 仅在 10.0.17763 之后才是真正的异步操作,但在此之前不是真正的。怎么解释?