从序列化数据中获取所有文件名

时间:2015-06-05 10:11:48

标签: php

我有一个序列化数据,其中有多个图像及其大小的记录。我想获取所有文件名,然后用我的数据替换它们。我已经使用序列化来完成它,但我无法获得所有文件名。

$s2mebership_details='a:5:{s:5:"width";i:200;s:6:"height";i:200;s:4:"file";s:27:"6month_project_training.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:4:{s:4:"file";s:33:"6month_project_training-80x80.jpg";s:5:"width";i:80;s:6:"height";i:80;s:9:"mime-type";s:10:"image/jpeg";}s:14:"responsive-100";a:4:{s:4:"file";s:35:"6month_project_training-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}s:14:"responsive-150";a:4:{s:4:"file";s:35:"6month_project_training-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:19:"badgeos-achievement";a:4:{s:4:"file";s:35:"6month_project_training-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}s:20:"sow-carousel-default";a:4:{s:4:"file";s:35:"6month_project_training-200x182.jpg";s:5:"width";i:200;s:6:"height";i:182;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:11:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";s:11:"orientation";i:0;}}';


$unserialize_me=unserialize($s2mebership_details);

echo $unserialize_me['file']; 

2 个答案:

答案 0 :(得分:1)

它们不属于第一个阵列:

$unserialize_me['file'];
$unserialize_me['sizes']['thumbnail']['file'];
$unserialize_me['sizes']['responsive-100']['file'];
$unserialize_me['sizes']['responsive-150']['file'];
$unserialize_me['sizes']['badgeos-achievement']['file'];
$unserialize_me['sizes']['sow-carousel-default']['file'];

如果你这样做:

print '<pre>';
print_r($unserialize_me);

您可以看到所有值。

编辑:

如果要在数组中更改它,例如:

$unserialize_me['sizes']['thumbnail']['file'] = 'my/file/location/filename.jpg'; 

再次打印__以查看更改

答案 1 :(得分:0)

$unserialize_me是多维数组,原始文件名位于$unserialize_me['file'],其他位于sizes键下。

$filenames = array();
$filenames[] = $unserialize_me['file']; // original filename
foreach ($unserialize_me['sizes'] as $file) {
    $filenames[] = $file['file'];       // thumbnails
}

print_r($filenames);

/*
    Array
    (
        [0] => 6month_project_training.jpg
        [1] => 6month_project_training-80x80.jpg
        [2] => 6month_project_training-100x100.jpg
        [3] => 6month_project_training-150x150.jpg
        [4] => 6month_project_training-100x100.jpg
        [5] => 6month_project_training-200x182.jpg
    )
*/

如果您需要钥匙,

$filenames = array();
$filenames['orig'] = $unserialize_me['file'];
foreach ($unserialize_me['sizes'] as $key => $file) {
    $filenames[$key] = $file['file'];
}

print_r($filenames);

/*
    Array
    (
        [orig] => 6month_project_training.jpg
        [thumbnail] => 6month_project_training-80x80.jpg
        [responsive-100] => 6month_project_training-100x100.jpg
        [responsive-150] => 6month_project_training-150x150.jpg
        [badgeos-achievement] => 6month_project_training-100x100.jpg
        [sow-carousel-default] => 6month_project_training-200x182.jpg
    )
*/