最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to extract data from a post meta serialized array?

programmeradmin1浏览0评论

I found an XML to WP decoder script that stores the data as an array in a custom meta field. What is the best way to extract the information?

For example how could I display the "Manufactured in" field as "CANADA"?

[_ttn_i_details] => Array ( [0] => a:5:{s:9:"engine_id";a:1:{i:0;s:9:"300000225";}s:15:"transmission_id";a:1:{i:0;s:6:"257691";}s:5:"plant";a:1:{i:0;s:23:"Oshawa, Ontario, Canada";}s:15:"Manufactured in";a:1:{i:0;s:6:"CANADA";}s:22:"Production Seq. Number";a:1:{i:0;s:6:"151411";}} )

The example code above was produced via print_r(get_post_custom($post->ID));.

I really appreciate any insight, no matter how small. :)

I found an XML to WP decoder script that stores the data as an array in a custom meta field. What is the best way to extract the information?

For example how could I display the "Manufactured in" field as "CANADA"?

[_ttn_i_details] => Array ( [0] => a:5:{s:9:"engine_id";a:1:{i:0;s:9:"300000225";}s:15:"transmission_id";a:1:{i:0;s:6:"257691";}s:5:"plant";a:1:{i:0;s:23:"Oshawa, Ontario, Canada";}s:15:"Manufactured in";a:1:{i:0;s:6:"CANADA";}s:22:"Production Seq. Number";a:1:{i:0;s:6:"151411";}} )

The example code above was produced via print_r(get_post_custom($post->ID));.

I really appreciate any insight, no matter how small. :)

Share Improve this question edited May 27, 2011 at 14:29 MTT asked May 27, 2011 at 14:21 MTTMTT 3,58612 gold badges47 silver badges74 bronze badges 1
  • 2 If your going to vote down a question at least be adult enough to say why. I'm trying hard to learn php. This may be a basic thing to most programmers but I'm coming from a design background. – MTT Commented May 27, 2011 at 16:59
Add a comment  | 

2 Answers 2

Reset to default 50

Use unserialize() to convert it into an array.

$mydata = 'a:5:{s:9:"engine_id";a:1:{i:0;s:9:"300000225";}s:15:"transmission_id";a:1:{i:0;s:6:"257691";}s:5:"plant";a:1:{i:0;s:23:"Oshawa, Ontario, Canada";}s:15:"Manufactured in";a:1:{i:0;s:6:"CANADA";}s:22:"Production Seq. Number";a:1:{i:0;s:6:"151411";}}';
$mydata = unserialize($mydata);
echo $mydata['Manufactured in'][0];

Edit- Related thought- something to keep in mind when storing meta data serialized like this is that you limit your ability to use that data in queries, if that's a concern for you. for instance, it's not so easy to write queries like "show me all parts manufactured in Canada", or order results by engine id, since that data is tucked away with a bunch of other data in one field.

You can use this WordPress codex which converts into an array.

maybe_unserialize($data);

https://developer.wordpress/reference/functions/maybe_unserialize/

发布评论

评论列表(0)

  1. 暂无评论