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. :)

  • 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

2 Answers 2


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.

  • @M.Taylor - you're welcome, check the edit I added about serialized data and queries, not sure it's a concern for you, but it may be.
    – Milo
    Commented May 27, 2011 at 14:54
  • You make a great point about losing the ability to search the data. I'll have to look into how to not store it in a serialized array, given that I didn't write the script.
    – MTT
    Commented May 27, 2011 at 15:07
  • @M.Taylor I don't, sorry. I'm sure someone here can help though.
    – Milo
    Commented May 27, 2011 at 15:32

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



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.