在 PHP 7.4 中执行 array_diff — 计算数组的差集 时,报错:Object of class ThemeVersion could not be converted to string
1、在 PHP 7.4 中执行 array_diff — 计算数组的差集 时,报错:Object of class ThemeVersion could not be converted to string。如图1
{ "message": "Object of class Modules\\ThemeStoreDb\\ThemeSetting\\ThemeVersion could not be converted to string", "status_code": 500, "debug": { "line": 299, "file": "E:\\wwwroot\\object\\Modules\\ThemeStoreDb\\Handlers\\ThemeHandler.php", "class": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError", "trace": [ "#0 E:\\wwwroot\\object\\Modules\\ThemeStoreDb\\Handlers\\ThemeHandler.php(299): array_diff(Array, Array)", ] } }
2、代码实现如下,分别打印两个比较的数组
print_r($versions); print_r($executedMigrations); exit; $scheduledVersions = array_diff($versions, $executedMigrations);
Array ( [0] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => 2023_07_07_10_custom_mobile_layout ) [1] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.0.20 ) [2] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.0.21 ) [3] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.0.28 ) [4] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.1 ) [5] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.2 ) [6] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.3 ) [7] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.30 ) [8] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.65 ) [9] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.70 ) [10] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.80 ) ) Array ( [0] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.0.20 ) [1] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.0.21 ) [2] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.0.28 ) [3] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.1 ) [4] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.2 ) [5] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.3 ) [6] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.30 ) )
3、调整为 array_udiff — 用回调函数比较数据来计算数组的差集。strcasecmp — 二进制安全比较字符串(不区分大小写)
//$scheduledVersions = array_diff($versions, $executedMigrations); $scheduledVersions = array_udiff($versions, $executedMigrations, function ($version, $executedMigration) { return strcasecmp($version->version, $executedMigration->version); });
4、最后的结果符合预期。
Array ( [0] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.65 ) [1] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.70 ) [2] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => v2.1.80 ) [3] => Modules\ThemeStoreDb\ThemeSetting\ThemeVersion Object ( [version] => 2023_07_07_10_custom_mobile_layout ) )
近期评论