我使用序列化方法将 image_url 以及标题和 alt 数据插入 MySQL 数据库,如下所示:
a:3:{i:0;a:4:{i:0;s:48:"uploads/media/galleries/nature/butterfly_001.jpg";i:1;s:43:"uploads/media/galleries/nature/IMG_4181.jpg";i:2;s:43:"uploads/media/galleries/nature/swan_002.jpg";i:3;s:45:"uploads/media/galleries/nature/zzmed copy.jpg";}i:1;a:4:{i:0;s:19:"test_title";i:1;s:20:"test_title1";i:2;s:25:"test_title2";i:3;s:24:"677777777777777777777777";}i:2;a:4:{i:0;s:19:"test_alt";i:1;s:21:"test_alt1";i:2;s:21:"test_alt2";i:3;s:26:"77888888888888888888888888";}}
PHP 代码:
$value['gallery_data'] = serialize((array(array_values($_POST['image_url']), array_values($_POST['image_title']), array_values($_POST['image_alt']))));
现在,我可以像这样打印 image_url :
$all1 = Access::fetch("SELECT * FROM " . GALLERIES . " WHERE id = ?", $id);
$array= unserialize($all1['0']['name']);
echo($array['0']['1']);
输出是:
uploads/media/galleries/nature/butterfly_001.jpg
但是,我需要打印每个 id 的所有 image_url、image_title 和 image_alt 列表,如下所示:
uploads/media/galleries/nature/butterfly_001.jpg - test_title - test_alt
uploads/media/galleries/nature/IMG_4181.jpg - test_title1 - test_alt1
uploads/media/galleries/nature/zzmed copy.jpg - test_title2 - test_alt2
如何在 foreach
方法中打印它?!
最佳答案
我的英语不好。所以,我把这段代码写给你。我希望这有帮助。
$un_arr = unserialize($arr);
$export = NULL;
array_walk_recursive($un_arr, function($val, $key) use (&$export ) {
$img_extension = array('.jpg','.jpeg');
foreach($extension as $value){
if(strpos($val,$value) != false){
$export .= $val . '<br>';
}
}
});
echo $export ;
如果这一行 (strpos($val,'.jpg') != false) 不起作用。您可以更改为 strpos($val,'.jpg') !== false。我希望这对你有帮助
我修复了许多扩展图像。希望这对您有帮助
关于php - 在 PHP 中打印多个反序列化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25878630/