Home  >  Article  >  类库下载  >  set collection type

set collection type

高洛峰
高洛峰Original
2016-10-20 14:31:465320browse

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"
    ]
]

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

Similarly, add an element to the set

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

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

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

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

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

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

smembers set-key // 返回'item2','item3'

PHP version demonstration

The first step is to link the redis database

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

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

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

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

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

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);

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)

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

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

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


Statement:
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

Related articles

See more