Home > Blockchain >  How to filter an array in PHP by fields with empty values and multiple criteria?
How to filter an array in PHP by fields with empty values and multiple criteria?

Time:01-12

My API call returns the following JSON output before json_decode:

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

The following code parses the multi-level json and shows the whole projects list:

$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>';
}

I got the following output:

00000001
A title
0.99
US
A text of a comment

00000002
Another title
1.03
US
Another text of a comment

The questions are:

  1. How to list available device types (the fields have names only and no values)?
  2. How to filter the array based on certain criteria (price must be equal or higher $1.00)?
  3. How to filter elements based on fields without values (show projects just for Android devices)?

CodePudding user response:

Question 1

to list all the available device types per project as a json you should access it's corresponding tree

foreach($result['projects'] as $project) {
    $project_device_types = $project['plateform_type'];
    echo '<pre>'.$project_device_types.'<pre>';
}

Question 2

to filter the array use [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'];
}

Question 3

you can follow the exact pattern as the second question and that is by using array_filter so eg. let's say you want only the android devices
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

  •  Tags:  
  • Related