php - 更改 PHP 序列化数据中所有唯一键的值
问题描述
这里 PHP 序列化(d)来自数据库的数据:
$data = 'a:5:{s:5:"width";i:700;s:6:"height";i:700;s:4:"file";s:22:"2020/11/fragrances.jpg";s:5:"sizes";a:8:{s:21:"woocommerce_thumbnail";a:5:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:0;}s:29:"woocommerce_gallery_thumbnail";a:4:{s:4:"file";s:22:"fragrances-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}s:18:"woocommerce_single";a:4:{s:4:"file";s:22:"fragrances-600x600.jpg";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:9:"thumbnail";a:4:{s:4:"file";s:22:"fragrances-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:12:"shop_catalog";a:5:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:0;}s:11:"shop_single";a:4:{s:4:"file";s:22:"fragrances-600x600.jpg";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:22:"fragrances-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}';
我需要将所有“文件”值更改为“new-fragrances.png”
$uns_data = unserialize($data);
$uns_data['file'] = 'new-fragrances.png';
$data = serialize($uns_data);
var_dump($data);
结果(它只改变了第一个键值):
'a:5:{s:5:"width";i:700;s:6:"height";i:700;s:4:"file";s:18:"new-fragrances.png";s:5:"sizes";a:8:{s:21:"woocommerce_thumbnail";a:5:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:0;}s:29:"woocommerce_gallery_thumbnail";a:4:{s:4:"file";s:22:"fragrances-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}s:18:"woocommerce_single";a:4:{s:4:"file";s:22:"fragrances-600x600.jpg";s:5:"width";i:600;s:6:'... (length=1571)
如何更改密钥文件的所有值?
解决方案
有很多方法可以遍历数组的每一项。在这种情况下,我会使用array_walk_recursive。在回调函数中array_walk_recursive
随心所欲地处理数组中每个项目的值。在此示例中,我替换fragrances
为new_fragrances
:
$data = 'a:5:{s:5:"width";i:700;s:6:"height";i:700;s:4:"file";s:22:"2020/11/fragrances.jpg";s:5:"sizes";a:8:{s:21:"woocommerce_thumbnail";a:5:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:0;}s:29:"woocommerce_gallery_thumbnail";a:4:{s:4:"file";s:22:"fragrances-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}s:18:"woocommerce_single";a:4:{s:4:"file";s:22:"fragrances-600x600.jpg";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:9:"thumbnail";a:4:{s:4:"file";s:22:"fragrances-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:12:"shop_catalog";a:5:{s:4:"file";s:22:"fragrances-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:0;}s:11:"shop_single";a:4:{s:4:"file";s:22:"fragrances-600x600.jpg";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:22:"fragrances-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}';
$uns_data = unserialize($data);
array_walk_recursive($uns_data, function (&$value, $key){
$value = str_replace('fragrances', 'new-fragrances', $value);
});
$data = serialize($uns_data);
var_dump($data);
请注意,$value
通过引用传递,使用&$value
,因此可以修改原始值。
推荐阅读
- next.js - 如何在 Nextjs 中更新 getServerSideProps 获取的 UI 数据
- c# - C# DataContract 强制 PascalCase 输出
- python - 为什么这个 html 页面中的 for 循环不起作用?
- python - 满足条件后如何更新pygame窗口显示?
- javascript - 我可以从 JavaScript 中的另一个对象调用属性吗?
- node.js - 无效的架构配置:`model` 不是数组中的有效类型
- sql - 选择由不同事物排序的 DISTINCT 行
- node.js - Puppeteer:Element.hover() 不存在
- c# - C# MonoGame 内存管理
- java - 如何生成 SAS 令牌并使用它将文件上传到 Azure Blob 存储?