在Wordpress中从数组中反序列化和调用数据

时间:2014-04-10 01:49:39

标签: php arrays wordpress serialization

Wordpress使用" _wp_attachment_metadata"存储有关您上传到Wordpress的视频的信息。分辨率,文件大小,格式等数据......我试图调用该数据并显示它。

数据是序列化数组:

a:13:{s:8:"lossless";b:0;s:7:"bitrate";i:6040338;s:12:"bitrate_mode";s:3:"cbr";s:8:"filesize";i:546209724;s:9:"mime_type";s:14:"video/x-ms-wmv";s:6:"length";i:695;s:16:"length_formatted";s:5:"11:34";s:5:"width";i:1280;s:6:"height";i:720;s:10:"fileformat";s:3:"asf";s:10:"dataformat";s:3:"wmv";s:7:"encoder";s:21:"Windows Media Video 9";s:5:"audio";a:12:{s:5:"codec";s:23:"Windows Media Audio 9.2";s:8:"channels";i:2;s:11:"sample_rate";i:44100;s:7:"bitrate";i:330258;s:15:"bits_per_sample";i:16;s:10:"dataformat";s:3:"wma";s:12:"bitrate_mode";s:3:"cbr";s:8:"lossless";b:0;s:7:"encoder";s:23:"Windows Media Audio 9.2";s:15:"encoder_options";s:41:"320 kbps, 44 kHz, stereo (A/V) 1-pass CBR";s:11:"channelmode";s:6:"stereo";s:17:"compression_ratio";d:0.2340263605442176875204296493393485434353351593017578125;}}

我提取数据的代码似乎有效:

$woo_files_number = get_post_meta( get_the_ID(), 'woo_files', true );
if( ! empty( $woo_files_number ) ) {
  $custom_fields = get_post_custom( $woo_files_number );
  $download_meta = $custom_fields['_wp_attachment_metadata'];
  }             
if( ! empty( $woo_files_number ) ) {
  echo $download_meta;
}

现在它显示了序列化数据。我对如何反序列化数据非常困惑,然后从中显示属性。当我通过在线反序列化程序运行它时,它向我显示序列化数组中包含的数据是:

Array
(
[lossless] => 
[bitrate] => 6040338
[bitrate_mode] => cbr
[filesize] => 546209724
[mime_type] => video/x-ms-wmv
[length] => 695
[length_formatted] => 11:34
[width] => 1280
[height] => 720
[fileformat] => asf
[dataformat] => wmv
[encoder] => Windows Media Video 9
[audio] => Array
(
[codec] => Windows Media Audio 9.2
[channels] => 2
[sample_rate] => 44100
[bitrate] => 330258
[bits_per_sample] => 16
[dataformat] => wma
[bitrate_mode] => cbr
[lossless] => 
[encoder] => Windows Media Audio 9.2
[encoder_options] => 320 kbps, 44 kHz, stereo (A/V) 1-pass CBR
[channelmode] => stereo
[compression_ratio] => 0.234026360544
)

所以我的问题是,我如何完成那些代码,反序列化数据,然后能够输出部分代码,如length_formatted,width,height等...... /强>

我一直在这里搜索几天试图破解并理解它,但我在我的智慧结束时似乎无法弄清楚最后一块。非常感谢你的帮助和时间!

1 个答案:

答案 0 :(得分:0)

你应该直接查询post meta而不是从get_post_custom中提取它。

如果您未使用任何其他自定义字段,则表示您正在使用未加载的数据。

而不是你的代码:

  if( ! empty( $woo_files_number ) ) {
     $custom_fields = get_post_custom( $woo_files_number );
     $download_meta = $custom_fields['_wp_attachment_metadata'];
  }   

直接获取post meta。

if( ! empty( $woo_files_number ) ) {
  $download_meta = get_post_meta(get_the_ID(), '_wp_attachment_metadata', true);
  // Print out the array in a nice format for easy reading. 
  echo '<pre>'; 
  print_r($download_meta);
  echo '</pre>; 
} 

下次在wordpress堆栈网站上发布此内容,您将获得更多点击量。