Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing serialized Array (Wordpress)

Tags:

php

wordpress

I'm using wordpress and one of my meta key values is stored like this:

a:1:{i:0;s:8:"Religion";}

I'm trying to figure out the easiest way in PHP to parse this so I can extract "Religion" or really any of the elements in a clean manner.

Hope this makes sense - thanks!!!

Loren

like image 379
99823 Avatar asked Jun 02 '26 04:06

99823


2 Answers

that is a serialized array, use unserialize()

$array = unserialize('a:1:{i:0;s:8:"Religion";}');
echo $array[0];
like image 199
Mihai Iorga Avatar answered Jun 03 '26 18:06

Mihai Iorga


This works in PHP 5.2 and above:

list($thatWord) = unserialize($metaKeyValue);

You then have the string "Religion" in $thatWord.

The string you have:

a:1:{i:0;s:8:"Religion";}

Is a serialized array. If you retrieve it from within wordpress, you would get an Array instead of a string. I assume you pull it from the database directly, so you need to unserializeDocs it your own.

like image 31
hakre Avatar answered Jun 03 '26 19:06

hakre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!