Вопрос

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

Это было полезно?

Решение

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.org/reference/functions/maybe_unserialize/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top