Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    
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. –  mattrepublic May 27 '11 at 16:59

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
Thank you @Milo that worked perfectly! –  mattrepublic May 27 '11 at 14:51
    
@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 May 27 '11 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. –  mattrepublic May 27 '11 at 15:07
    
@M.Taylor I don't, sorry. I'm sure someone here can help though. –  Milo May 27 '11 at 15:32
    
No worries! Thanks again. –  mattrepublic May 27 '11 at 15:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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