-1

I have the following json string which is stored in my database:

{"Monomeer5e68d7e547620":{"naam":"Monomeer","url":"https:\/\/website.nl\/new\/folie\/monomeer","afbeelding":"assets\/images\/noimg.jpg","aantal":"5","prijs":"2.28","totaalprijs":"20.4","hoogte":"20","breedte":"20","uploaden":"1","specificaties":"Array","controle":"1"},"Elastieken5e68d7e5477b6":{"naam":"Elastieken","url":"https:\/\/website.nl\/new\/folie\/monomeer","afbeelding":"assets\/images\/noimg.jpg","aantal":"1","prijs":"4.50","totaalprijs":"4.5","hoogte":"","breedte":"","uploaden":"0","specificaties":"","controle":""},"Tiewraps5e68d7e5477c8":{"naam":"Tiewraps","url":"https:\/\/website.nl\/new\/folie\/monomeer","afbeelding":"assets\/images\/noimg.jpg","aantal":"1","prijs":"9.95","totaalprijs":"9.95","hoogte":"","breedte":"","uploaden":"0","specificaties":"","controle":""}}

It contains product information that is otherwise shown by a session which looks like this:

Array
(
    [Monomeer5eeb62474c5ba] => Array
        (
            [naam] => Monomeer
            [url] => https://website.nl/new/folie/monomeer
            [afbeelding] => assets/images/noimg.jpg
            [aantal] => 1
            [hoogte] => 5
            [breedte] => 11
            [uploaden] => 1
            [specificaties] => Array
                (
                    [Lijmlaag] => Array
                        (
                            [waarde] => Wit
                        )

                    [Laminaat] => Array
                        (
                            [waarde] => Mat laminaat
                        )

                    [Afwerking] => Array
                        (
                            [waarde] => Contoursnijden
                        )

                )

            [prijs] => 0.3355
            [totaalprijs] => 0.3355
        )

    [Elastieken5eeb62474c74f] => Array
        (
            [naam] => Elastieken
            [url] => https://website.nl/new/folie/monomeer
            [afbeelding] => assets/images/noimg.jpg
            [aantal] => 1
            [prijs] => 4.50
            [totaalprijs] => 4.5
            [uploaden] => 0
        )

    [Tiewraps5eeb62474c760] => Array
        (
            [naam] => Tiewraps
            [url] => https://website.nl/new/folie/monomeer
            [afbeelding] => assets/images/noimg.jpg
            [aantal] => 1
            [prijs] => 9.95
            [totaalprijs] => 9.95
            [uploaden] => 0
        )

)

When the session is expired, I want someone to be able to look up their order information again, so on the page that displays this I am trying to build a check (see if session exists, if yes loop over the session, if not loop over the php array that is exactly the same as the session). But to get the same array as my session it needs to be converted from a JSON string.

How can I do that? Convert the above JSON string to a PHP array that looks the same as my PHP session contents.

When I try decoding the json string, this is what I get:

stdClass Object ( 
    [Monomeer5ee48a5bddfd1] => stdClass Object ( 
            [naam] => Monomeer 
            [url] => https://website.nl/new/folie/monomeer [afbeelding] => assets/images/noimg.jpg 
            [aantal] => 4 
            [prijs] => 1.785 
            [totaalprijs] => 7.14 
            [hoogte] => 5 
            [breedte] => 10 
            [uploaden] => 1 
            [specificaties] => Array [controle] => 
    ) 
    [Elastieken5ee48a5bde03a] => stdClass Object ( 
            [naam] => Elastieken 
            [url] => https://website.nl/new/folie/monomeer 
            [afbeelding] => assets/images/noimg.jpg 
            [aantal] => 1 
            [prijs] => 4.50 
            [totaalprijs] => 4.5 
            [hoogte] => 
            [breedte] => 
            [uploaden] => 0 
            [specificaties] => [controle] => 
    ) 
    [Tiewraps5ee48a5bde048] => stdClass Object ( 
            [naam] => Tiewraps 
            [url] => https://website.nl/new/folie/monomeer 
            [afbeelding] => assets/images/noimg.jpg 
            [aantal] => 1 
            [prijs] => 9.95 
            [totaalprijs] => 9.95 
            [hoogte] => 
            [breedte] => 
            [uploaden] => 0 
            [specificaties] => [controle] => 
    ) 
)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
twan
  • 2,450
  • 10
  • 32
  • 92
  • Unfortunately what is stored in your database is missing the actual contents of the `specfications` element (it looks like it was echo'ed instead of copied). But you can convert it using `json_decode($json, true)` to get it close to your desired output – Nick Jun 18 '20 at 12:58

1 Answers1

1

You must use the second argument of the json_decode function which will actually decode your JSON string into an array, instead of a stdClass object.

$array = json_decode($json, true);

From the docs:

json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] ) : mixed

assoc When TRUE, returned objects will be converted into associative arrays.

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50