Here PHP serialize(d) data from database:
$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:{}}}';
I need to change all "file" value to "new-fragrances.png"
$uns_data = unserialize($data);
$uns_data['file'] = 'new-fragrances.png';
$data = serialize($uns_data);
var_dump($data);
Resut (It only changed the first key value):
'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)
How can I change all value of the key file?
My Answer:
"apple";s:5:"red";s:6:"banana";s:6:"yellow";s:5:"grape";s:5:"green";s:6:"orange";s:6:"orange";s:4:"pear";s:4:"brown";}';
To change the value in all unique keys, you can unserialize the data, modify the values, and then serialize it back. Here's an example code snippet to change the value in all unique keys:
php
$data = 'a:5:{s:5:"apple";s:5:"red";s:6:"banana";s:6:"yellow";s:5:"grape";s:5:"green";s:6:"orange";s:6:"orange";s:4:"pear";s:4:"brown";}';
// Unserialize the data
$unserializedData = unserialize($data);
// Modify the values in all unique keys
foreach ($unserializedData as $key => $value) {
$unserializedData[$key] = strtoupper($value); // Change the value to uppercase
}
// Serialize the modified data back
$serializedData = serialize($unserializedData);
echo $serializedData;
This code snippet will change the value in all unique keys to uppercase. You can modify the values in the foreach loop according to your requirements.
Rate this post
5 of 5 based on 3120 votesComments