Home > Net >  How to parse WordPress wpDataTable data using PHP?
How to parse WordPress wpDataTable data using PHP?

Time:03-23

I have the below formatted code from database.

a:9:{i:8053;a:26:{s:4:"name";s:0:"";s:12:"squareseaten";s:0:"";s:4:"wins";s:0:"";s:9:"timetaken";s:0:"";s:10:"tubeseaten";s:0:"";s:12:"gummieseaten";s:0:"";s:12:"pepperseaten";s:0:"";s:10:"chipseaten";s:0:"";s:13:"bottlechugged";s:1:"1";s:17:"boxesofbiteseaten";s:0:"";s:19:"boxesofbrutalseaten";s:0:"";s:14:"crunchieseaten";s:0:"";s:17:"boxesofdncvseaten";s:0:"";s:15:"bottlesconsumed";s:0:"";s:12:"pieceschewed";s:0:"";s:14:"additionalnuts";s:1:"1";s:15:"additionaltimes";s:0:"";s:18:"additionalattempts";s:0:"";s:3:"pts";s:0:"";s:1:"p";s:0:"";s:1:"w";s:0:""; ...

This looks like JSON. But it's not actually. This was actually generated by a WordPress plugin called wpDataTables.

How can I parse through this data in PHP?

CodePudding user response:

That's a serialized string. Run that string through unserialize() and you'll get an array.

CodePudding user response:

This is a serialized string you need to use unserialize() Ex:

<?php
$data = 'a:3:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;s:4:"Blue";}';

$test = unserialize($data);
var_dump($test);
?>

Output

array(3) { [0]=> string(3) "Red" [1]=> string(5) "Green" [2]=> string(4) "Blue" }

The unserialize() function converts serialized data into array.

Syntax:

unserialize(string, options);

string(Required): Specifies the serialized string.

options : Specifies options to be provided to the function, as an associative array. Can be either an array of class names which should be accepted, false to accept no classes, or true to accept all classes. True is default.

  • Related