9951 explained code solutions for 126 technologies


php-redisGet nested JSON from Redis key


If our JSON is stored as string:

$json = json_decode($redis->get('js'), 1);ctrl + c
$redis

Redis object after connection

get

get string key value

'js'

name of the key with our saved JSON string

json_decode

decodes JSON string into associative array


Usage example

<?php

$r = new Redis(); 
$r->connect('127.0.0.1', 6379);

$r->set('js', json_encode(['name' => 'Donald', 'top' => [1, 2, 3]]));

print_r(json_decode($r->get('js'), 1));
output
Array
(
    [name] => Donald
    [top] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)