访问php中的序列化数组 - WORDPRESS POSTMETA

时间:2017-12-28 18:53:59

标签: php arrays wordpress serialization

我有一个需要访问的序列化数组: -

a:5:{s:5:"width";i:750;s:6:"height";i:330;s:4:"file";s:25:"2017/12/Sophrologie-1.jpg";s:5:"sizes";a:21:{s:20:"listingpro-blog-grid";a:4:{s:4:"file";s:25:"Sophrologie-1-372x240.jpg";s:5:"width";i:372;s:6:"height";i:240;s:9:"mime-type";s:10:"image/jpeg";}s:21:"listingpro-blog-grid2";a:4:{s:4:"file";s:25:"Sophrologie-1-372x330.jpg";s:5:"width";i:372;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:21:"listingpro-blog-grid3";a:4:{s:4:"file";s:25:"Sophrologie-1-672x330.jpg";s:5:"width";i:672;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:23:"listingpro-listing-grid";a:4:{s:4:"file";s:25:"Sophrologie-1-272x231.jpg";s:5:"width";i:272;s:6:"height";i:231;s:9:"mime-type";s:10:"image/jpeg";}s:26:"listingpro-listing-gallery";a:4:{s:4:"file";s:25:"Sophrologie-1-580x330.jpg";s:5:"width";i:580;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:21:"listingpro-list-thumb";a:4:{s:4:"file";s:25:"Sophrologie-1-287x190.jpg";s:5:"width";i:287;s:6:"height";i:190;s:9:"mime-type";s:10:"image/jpeg";}s:23:"listingpro-author-thumb";a:4:{s:4:"file";s:23:"Sophrologie-1-63x63.jpg";s:5:"width";i:63;s:6:"height";i:63;s:9:"mime-type";s:10:"image/jpeg";}s:25:"listingpro-gallery-thumb1";a:4:{s:4:"file";s:25:"Sophrologie-1-458x330.jpg";s:5:"width";i:458;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:25:"listingpro-gallery-thumb2";a:4:{s:4:"file";s:25:"Sophrologie-1-360x198.jpg";s:5:"width";i:360;s:6:"height";i:198;s:9:"mime-type";s:10:"image/jpeg";}s:25:"listingpro-gallery-thumb3";a:4:{s:4:"file";s:25:"Sophrologie-1-263x198.jpg";s:5:"width";i:263;s:6:"height";i:198;s:9:"mime-type";s:10:"image/jpeg";}s:25:"listingpro-gallery-thumb4";a:4:{s:4:"file";s:25:"Sophrologie-1-653x199.jpg";s:5:"width";i:653;s:6:"height";i:199;s:9:"mime-type";s:10:"image/jpeg";}s:25:"listingpro-detail_gallery";a:4:{s:4:"file";s:25:"Sophrologie-1-383x330.jpg";s:5:"width";i:383;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:33:"listingpro-checkout-listing-thumb";a:4:{s:4:"file";s:24:"Sophrologie-1-220x80.jpg";s:5:"width";i:220;s:6:"height";i:80;s:9:"mime-type";s:10:"image/jpeg";}s:31:"listingpro-review-gallery-thumb";a:4:{s:4:"file";s:25:"Sophrologie-1-184x135.jpg";s:5:"width";i:184;s:6:"height";i:135;s:9:"mime-type";s:10:"image/jpeg";}s:17:"listingpro-thumb4";a:4:{s:4:"file";s:25:"Sophrologie-1-272x300.jpg";s:5:"width";i:272;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:26:"listingpro_location270_400";a:4:{s:4:"file";s:25:"Sophrologie-1-270x330.jpg";s:5:"width";i:270;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:26:"listingpro_location570_455";a:4:{s:4:"file";s:25:"Sophrologie-1-570x330.jpg";s:5:"width";i:570;s:6:"height";i:330;s:9:"mime-type";s:10:"image/jpeg";}s:26:"listingpro_location570_228";a:4:{s:4:"file";s:25:"Sophrologie-1-570x228.jpg";s:5:"width";i:570;s:6:"height";i:228;s:9:"mime-type";s:10:"image/jpeg";}s:26:"listingpro_location270_197";a:4:{s:4:"file";s:25:"Sophrologie-1-270x197.jpg";s:5:"width";i:270;s:6:"height";i:197;s:9:"mime-type";s:10:"image/jpeg";}s:22:"listingpro_cats270_213";a:4:{s:4:"file";s:25:"Sophrologie-1-270x213.jpg";s:5:"width";i:270;s:6:"height";i:213;s:9:"mime-type";s:10:"image/jpeg";}s:22:"Sophrologie-1-1170x400";a:5:{s:4:"file";s:25:"Sophrologie-1-1170x400jpg";s:5:"width";s:4:"1170";s:6:"height";s:3:"400";s:9:"mime-type";s:10:"image/jpeg";s:7:"nova-wp";b:1;}}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:19:"Maygutyak - Fotolia";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:{}}}

我想从中检索Sophrologie-1-1170x400jpg文件名(在数组的底部)。

我无法弄清楚如何将此文件名检索到此数组中。线索是该行中有nova-wp值。

此数组来自wordpress安装中的wp-postmeta表。

如果你能指引我,我将非常感激。

1 个答案:

答案 0 :(得分:1)

您显示的数据是通过序列化生成的。使用unserialize function将其恢复到原始状态:

$data = unserialize($text);

然后,遍历反序列化的数据,以便检索您要查找的值:

echo $data["sizes"]["Sophrologie-1-1170x400"]["file"];
// Output: Sophrologie-1-1170x400jpg

可以找到工作演示here

如果您需要扫描数据以找到正确的值,可以使用以下代码:

$data = unserialize($text);

if (array_key_exists("Sophrologie-1-1170x400", $data["sizes"])) {
    echo $data["sizes"]["Sophrologie-1-1170x400"]["file"];
} else {
    echo "File not found!";
}