Home 类库下载 PHP类库 set collection type

set collection type

Oct 20, 2016 pm 02:31 PM

3. Set collection type

Introduction

Set collections are very similar to lists and can store multiple strings. However, the list can store duplicate values, but the set cannot be repeated.

Set structure

where user is the key name, which can contain multiple non-repeating elements, each different

[
    "user":[
        "yanying1",
        "yanying2",
        "yanying3"
    ]
]
Copy after login

Operation method

set collection type

Demo (command line + PHP demo)

Command line:

Add an element to the set and return 1 to indicate successful insertion; 0 element indicates that it already exists

sadd set-key item // 返回 1
Copy after login

Similarly, add an element to the set

sadd set-key item2 // 返回1,表示插入成功,集合内没有该元,
sadd set-key item3 // 返回
1sadd set-key item // 返回0,表示插入失败,集合内已经存在
Copy after login

After adding it, we will get all the elements in the set and return all elements successfully

smembers set-key // 得到结果:'item','item2','item3',由于元素不能重复,所以最后一个item不存在集合中
Copy after login

The following introduces the important functions of this collection. Use the sismember command to check whether the element is in the collection. If it exists, it returns 1, if it does not exist, it returns 0

sismember set-key item4 // 由于item4不在集合中,所以返回 0
sismember set-key item // item元素是存在于集合中的,返回1;同理item2,item3也是会返回1
Copy after login

Now we try to delete an element. If the deletion is successful, the number of the deleted element will be returned. Count

srem set-key item //删除成功,返回删除个数:1
srem set-key item // item之前已经被删除,所以删除失败,返回个数0。这里不是表示true or false
Copy after login

We check all elements again and find that only item2 and item3 are left

smembers set-key // 返回'item2','item3'
Copy after login

PHP version demonstration

The first step is to link the redis database

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
Copy after login

We first insert an element item into the set-key collection. 1 represents successful insertion; 0 element represents that it already exists

$redis->sadd('set-key','item'); // 添加成功,返回1
Copy after login

Next, we add the item to the set-key collection again and find that it already exists, and 0

$redis->sadd('set-key','item'); // 集合中已经存在item,添加失败,返回0
Copy after login

is returned. For the following demonstration, we are adding several elements

$redis->sadd('set-key','item2');
$redis->sadd('set-key','item3');
Copy after login

Now there are 3 elements in the collection, we use the smembers command to take them all out

$r = $redis->smembers('set-key');
var_dump($r);
Copy after login

After printing, we get an array containing item, item2, item3

array (size=3)
  0 => string 'item' (length=4)
  1 => string 'item2' (length=5)
  2 => string 'item3' (length=5)
Copy after login

Next we use the sismember command to check whether an element exists in the set , this is very useful when checking whether a nickname already exists

$redis->sismember('set-key','item'); //该元素存在集合中,则为 boolean true,反之boolean false
Copy after login

Finally, delete an element. Assume it is an item. If it is deleted for the first time and the element exists in the set, the number of deleted items will be returned: 1

$redis->srem('set-key','item'); // int 1
Copy after login

At this time, we execute the delete command again. Since the element no longer exists in the set, it will return 0 deleted items

$redis->srem('set-key','item'); // int 0
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)