Mysql:从 PHP 序列化的字段中获取所有不同的值 [英] Mysql: get all distinct values from field serialized by PHP
问题描述
我有一个 movies
表(包含来自遗留项目的数据),其字段 genre
包含由 PHP 序列化的值,例如:
I have a movies
table (has data from a legacy project) with the field genre
which contains values serialized by PHP like:
a:3:{i:0;s:9:"动画";i:1;s:9:"冒险";i:2;s:5:"戏剧";}
a:3:{i:0;s:9:"Animation";i:1;s:9:"Adventure";i:2;s:5:"Drama";}
我在搜索页面工作,&我需要找到当前搜索结果的所有独特流派作为页面过滤器,
I'm working in a search page, & I need to find all unique genres of the current search result to be used as a filter in the page,
例如,如果搜索结果是这 2 部电影:
as an example, if the search result was these 2 movies:
- 黑暗骑士(动作、犯罪、戏剧)
- 黑骑士(奇幻、冒险、喜剧)
我想知道这些流派的组合,这将是:
I want to know the combination of there genres, which will be:
['action', 'crime', 'drama', 'fantasy', 'adventure', 'comedy']
如何获取流派数组?(我正在使用 Yii2).
how to get the genres array? (I'm using Yii2).
推荐答案
你应该反序列化 你的数据
$data = 'a:3:{i:0;s:9:"Animation";i:1;s:9:"Adventure";i:2;s:5:"Drama";}';
$data = unserialize($data);
print_r($data);
你会得到
Array
(
[0] => Animation
[1] => Adventure
[2] => Drama
)
如果您需要在整个表格中搜索戏剧"来决定要显示哪些节目/电影,您可以始终在搜索中使用通配符
If you need to search the entire table for "Drama" to decide which shows/movies to display, you could always use wildcards in your search
select * from table where column like '%Drama%'
但当然要确保采取适当的数据库预防措施.
but of course make sure to take appropriate database precautions.
这篇关于Mysql:从 PHP 序列化的字段中获取所有不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!