如何按具有空值和多个条件的字段过滤 PHP 中的数组?
How to filter an array in PHP by fields with empty values and multiple criteria?
我的 API 在 json_decode
之前调用 returns 以下 JSON 输出:
{
"projects": [
{
"project_id": 00000001,
"name": "A title",
"price": "0.99",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "A text of a comment"
}
{
"project_id": 00000002,
"name": "Another title",
"price": "1.03",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "Another text of a comment"
}
]
}
以下代码解析多级 json 并显示整个项目列表:
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach($result['projects'] as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
$country = $project['country'];
#no values for the fields, that's why commented
#$android_phone = $project['platform_types']['android_phone'];
#$ios_phone = $project['platform_types']['ios_phone'];
#$ios_tablet = $project['platform_types']['ios_tablet'];
#$android_kindle = $project['platform_types']['android_kindle'];
#$android_tablet = $project['platform_types']['android_tablet'];
#$desktop = $project['platform_types']['desktop'];
$comment = $project['comment'];
echo $project_id,'<br>',$name,'<br>',$price,'<br>',$country,'<br>',$comment,'<br>';
}
我得到以下输出:
00000001
A title
0.99
US
A text of a comment
00000002
Another title
1.03
US
Another text of a comment
问题是:
- 如何列出可用的设备类型(字段只有名称而没有值)?
- 如何根据特定条件过滤数组(价格必须等于或高于 1.00 美元)?
- 如何根据没有值的字段过滤元素(仅显示 Android 设备的项目)?
问题 1
要将每个项目的所有可用设备类型作为 json 列出,您应该访问它对应的树
foreach($result['projects'] as $project) {
$project_device_types = $project['plateform_type'];
echo '<pre>'.$project_device_types.'<pre>';
}
问题 2
过滤数组使用 [array_filter][1] in
$filtered = array_filter($result['projects'], function($project) {
if($project['price'] >= 90) {
return true
}
});
// use it instead of the $result variable
foreach($filtered as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
}
问题 3
您可以按照第二个问题的确切模式进行操作,即使用 array_filter ,例如。假设您只需要 android 台设备
array_filter($result['projects'], function($project) {
if(in_array($project['platform_types'], "Android")) { // check if the plateform type include android by using [in_array][1]
return true
}
});
NB: in_array is case sensitive so make sure that the capitalization is correct
我的 API 在 json_decode
之前调用 returns 以下 JSON 输出:
{
"projects": [
{
"project_id": 00000001,
"name": "A title",
"price": "0.99",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "A text of a comment"
}
{
"project_id": 00000002,
"name": "Another title",
"price": "1.03",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "Another text of a comment"
}
]
}
以下代码解析多级 json 并显示整个项目列表:
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach($result['projects'] as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
$country = $project['country'];
#no values for the fields, that's why commented
#$android_phone = $project['platform_types']['android_phone'];
#$ios_phone = $project['platform_types']['ios_phone'];
#$ios_tablet = $project['platform_types']['ios_tablet'];
#$android_kindle = $project['platform_types']['android_kindle'];
#$android_tablet = $project['platform_types']['android_tablet'];
#$desktop = $project['platform_types']['desktop'];
$comment = $project['comment'];
echo $project_id,'<br>',$name,'<br>',$price,'<br>',$country,'<br>',$comment,'<br>';
}
我得到以下输出:
00000001
A title
0.99
US
A text of a comment
00000002
Another title
1.03
US
Another text of a comment
问题是:
- 如何列出可用的设备类型(字段只有名称而没有值)?
- 如何根据特定条件过滤数组(价格必须等于或高于 1.00 美元)?
- 如何根据没有值的字段过滤元素(仅显示 Android 设备的项目)?
问题 1
要将每个项目的所有可用设备类型作为 json 列出,您应该访问它对应的树
foreach($result['projects'] as $project) {
$project_device_types = $project['plateform_type'];
echo '<pre>'.$project_device_types.'<pre>';
}
问题 2
过滤数组使用 [array_filter][1] in
$filtered = array_filter($result['projects'], function($project) {
if($project['price'] >= 90) {
return true
}
});
// use it instead of the $result variable
foreach($filtered as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
}
问题 3
您可以按照第二个问题的确切模式进行操作,即使用 array_filter ,例如。假设您只需要 android 台设备array_filter($result['projects'], function($project) {
if(in_array($project['platform_types'], "Android")) { // check if the plateform type include android by using [in_array][1]
return true
}
});
NB: in_array is case sensitive so make sure that the capitalization is correct