首页 > 解决方案 > 如何从 mysql 和 php 中的 json 数组中搜索数据?

问题描述

{"email": "afadf%40gmail.com", 
"phone": "2525", 
"rooms": "4", 
"floors": "2", 
"user_id": "2", 
"ad_title": "sdafasfdasf", 
"features": "['Elevator','Center Stairs']", 
"car_space": "5", 
"full_name": "adaf", 
"sortOrder": "10000", 
"build_year": "2018"
}

我想使用上述 json 格式搜索 features='Elevator' 的数据。

标签: phpmysql

解决方案


你可以使用json_decode()函数。此函数将从 JSON 返回一个数组。

前任:

$data = '{
    "email": "afadf%40gmail.com", 
    "phone": "2525", 
    "rooms": "4", 
    "floors": "2", 
    "user_id": "2", 
    "ad_title": "sdafasfdasf", 
    "features": "['Elevator','Center Stairs']", 
    "car_space": "5", 
    "full_name": "adaf", 
    "sortOrder": "10000", 
    "build_year": "2018"
}';
$arr = json_decode($data);
//The $arr variable will be an array contains the data from $data variable

然后,您可以使用array_search()功能进行搜索。一些参考资料:

堆栈溢出

W3学校


推荐阅读