首页 > 解决方案 > 更改 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)

如何更改密钥文件的所有

标签: php

解决方案


有很多方法可以遍历数组的每一项。在这种情况下,我会使用array_walk_recursive。在回调函数中array_walk_recursive随心所欲地处理数组中每个项目的值。在此示例中,我替换fragrancesnew_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,因此可以修改原始值。


推荐阅读