如何从mysql和php中的json数组中搜索数据?

时间:2018-07-12 10:32:31

标签: php mysql

{"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'的数据。

1 个答案:

答案 0 :(得分:-1)

您可以使用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()函数进行搜索。一些参考:

Stack Overflow

W3Schools