doulongti5932 2017-02-17 06:18
浏览 58
已采纳

PHP:相同对象和数组失败的`strcmp`序列化

I have a legacy app where i am trying to migrate changes from the old into the new while generating a log of changes. Things are going well; however, I keep running into "changes" that change nothing. After digging into this, I found that the legacy code is using arrays and the new code is using objects. If serialized, I thought they would be identical. After all, if they are dumped via print_r they are identical. But that is not the case. Even more astounding, the objects keep their integer keys even after serialize-unserialize cycling them.

The request is: how can I show these two strings are identical since their resulting object/array is identical save for key typing.

<?php
  $v3v = 'a:2:{s:9:"lastindex";s:1:"1";i:1;s:1:"1";}';
  $v4v = 'a:2:{s:9:"lastindex";i:1;i:1;s:1:"1";}';
  $v3 = unserialize($v3v);
  $v4 = unserialize($v4v);
  die('<pre>'.print_r($v3,true).' '.print_r($v4,true));

outputs (the identical):

Array ( [lastindex] => 1 [1] => 1 ) Array ( [lastindex] => 1 [1] => 1 )

so let's now bring them "back to life":

$v3v = serialize($v3);
$v4v = serialize($v4);
die('<pre>'.print_r($v3v,true).PHP_EOL.print_r($v4v,true));

whaaa? how did you remember your integer keys??"

a:2:{s:9:"lastindex";s:1:"1";i:1;s:1:"1";}
a:2:{s:9:"lastindex";i:1;i:1;s:1:"1";}

and how can i get you to stop???

  • 写回答

1条回答 默认 最新

  • doufei7464 2017-02-17 06:34
    关注

    You can use array_diff instead of strcmp. You can try this -

      $v3v = 'a:2:{s:9:"lastindex";s:1:"1";i:1;s:1:"1";}';
      $v4v = 'a:2:{s:9:"lastindex";i:1;i:1;s:1:"1";}';
      $v3 = unserialize($v3v);
      $v4 = unserialize($v4v);
      echo empty(array_diff($v3, $v4)) ? 'Identical' : 'Not Identical';
    

    array_diff($v3, $v4) will return empty array if they are indentical.

    Working code

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

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件