Redis access to PHP multidimensional arrays example

Source: Internet
Author: User
Tags arrays redis


PHP array is actually a hash table, Redis does not seem to support two-dimensional arrays, but you can use Hmset to save the PHP array as a hash type of data, using Hmget to read a one-dimensional key is no problem, reading two-dimensional multidimensional keys return false.

$hset = Array (

' wust ' => ' WUHAN SI ',

' birth ' =>1031,

' Marray ' =>array ' (

' apple ' => ' apples '), 

' orange ' => ' org ',)

;

Dump ($redis->hmset (' Tuntun ', $hset))//save array as hash type

dump ($redis->hmget (' Tuntun ', Array (' birth ')) ;//Fetch the value of an array birth, OK

dump ($redis->hmget (' Tuntun ', Array (' Marry ', ' apple '));//Remove the value of apple from the two-dimensional array marray. return False

Even if you use the Hgetall method:

Dump ($redis->hgetall (' Hset_tuntun '));
The results are as follows:

Array (3) {
["Wust"] => string (8) "WUHAN SI"
["Birth"] => string (4) "1031"
["Marray"] => string (5) "Array"
}

Two-dimensional arrays cannot be displayed. So Redis does not support access to multidimensional arrays of PHP.

The solution is: if you want to use Redis to save multidimensional arrays, you can convert the array json_encode into JSON-type data, stored in string style. Read the time again Json_decode back.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.