0

In a DB table from wordpress (Caldera Form plugin), there's a JSON array as value, and from a query, result has this structure :

Array (
    [0] => stdClass Object (
        [config] => a:22:{
            s:2:"ID";
            s:15:"CF605df92fa7a17";
            s:13:"_last_updated";
            s:31:"Sat, 27 Mar 2021 16:44:15 +0000";
            s:10:"cf_version";
            s:5:"1.9.4";
            s:4:"name";
            s:29:"Registration for Artists 2021";
            s:10:"scroll_top";...}
        ) 
    [1] => stdClass Object (
        [config] => a:22:{
            s:2:"ID";
            s:15:"CF605ba79300239";
            s:13:"_last_updated";
            s:31:"Sat, 27 Mar 2021 16:44:51 +0000";
            s:10:"cf_version";
            s:5:"1.9.4";
            s:4:"name";
            s:29:"Inscription des Artistes 2021";
            s:10:"scroll_top";...}
        ) 

How can i read "a:22" then "s:15" inside ? I done a json_decode with [config] value but it return a empty value..

Steph TwX
  • 59
  • 2
  • 10
  • Is this the exact `var_dump` output? Maybe this might help... https://stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php/28538844 – joshmoto Mar 28 '21 at 01:07
  • yes, it's a print_r precisely. the problem is that indexes are not strings ( s:2 ... s:29.. ), so i don't know how to call them, i don't know what means " : " as index – Steph TwX Mar 28 '21 at 01:14
  • 1
    I'm think it might be serialised data, trying to decode it. – joshmoto Mar 28 '21 at 01:18
  • `json_decode($entries, true);` -> print_r is empty and `json_decode(json_encode($entries), true);` -> print_r gives the same array – Steph TwX Mar 28 '21 at 01:25

2 Answers2

0

Using this array dump you posted, what does this return?

foreach ($array as $key => $object) {

    $config = unserialize($object->config);

    var_dump($config);

}

Pass the same variable you dumped through $array. This might not work but worth a shot.




For example, if I have this $array dumped data...

Array
(
    [0] => stdClass Object
        (
            [config] => a:4:{s:2:"ID";s:15:"CF605df92fa7a17";s:13:"_last_updated";s:31:"Sat, 27 Mar 2021 16:44:15 +0000";s:10:"cf_version";s:5:"1.9.4";s:4:"name";s:29:"Registration for Artists 2021";}
        )

    [1] => stdClass Object
        (
            [config] => a:4:{s:2:"ID";s:15:"CF605ba79300239";s:13:"_last_updated";s:31:"Sat, 27 Mar 2021 16:44:51 +0000";s:10:"cf_version";s:5:"1.9.4";s:4:"name";s:29:"Inscription des Artistes 2021";}
        )

)

And run the above unserialize() function in loop, this is the output...

Array
(
    [ID] => CF605df92fa7a17
    [_last_updated] => Sat, 27 Mar 2021 16:44:15 +0000
    [cf_version] => 1.9.4
    [name] => Registration for Artists 2021
)
Array
(
    [ID] => CF605ba79300239
    [_last_updated] => Sat, 27 Mar 2021 16:44:51 +0000
    [cf_version] => 1.9.4
    [name] => Inscription des Artistes 2021
)



Response to your last comment, what does this output... ?

foreach ($array as $key => $object) {

    var_dump($object);

    // $config = unserialize($object->config);

    // var_dump($config);

}
joshmoto
  • 4,472
  • 1
  • 26
  • 45
  • your last foreach outputs the same array: `object(stdClass)#13684 (1) { ["config"]=> string(32718) "a:22:{s:2:"ID";s:15:"CF605df92fa7a17"; ....` – Steph TwX Mar 28 '21 at 02:11
  • @StephTwX yeah its because my test array values are the same, as I do not have your actual php returned array, only your dump which wont work, because the serialised config data is truncated/incomplete. – joshmoto Mar 28 '21 at 02:12
  • @StephTwX updated answer to your real data, if my answer helped please consider accepting answer to show question is solved thanks – joshmoto Mar 28 '21 at 02:28
0

you put me on the track

$results = [];
$data = [];
foreach ($entries as $key => $object) {

    $results[] = unserialize($object->config);
    foreach ($results as $key => $value) {
        $data = [
            'id' => $value['ID'],
            'name' => $value['name']
        ];
    }

    var_dump($data);

}```

gives :
array(2) { ["id"]=> string(15) "CF605df92fa7a17" ["name"]=> string(29) "Registration for Artists 2021" } 
array(2) { ["id"]=> string(15) "CF605ba79300239" ["name"]=> string(29) "Inscription des Artistes 2021" } 
It's what i need
So I can continue
Thanxs
Steph TwX
  • 59
  • 2
  • 10
  • Sweet! You have unserialized it to an array, good job! The clue to spot the difference between `json` and `serialised` strings, is that json starts with an opening brace `{`, though i'm sure you wont forget the difference now. Serialised strings are pretty obvious when you know, but even I thought your dump was json until I started debugging it, then realised lol. Anyways happy coding! – joshmoto Mar 28 '21 at 02:08
  • yes thanx, than i know what is `serialised`. i will give my previous post as soluce on tomorrow – Steph TwX Mar 28 '21 at 15:34