0

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)?
5
  • You should have a look at php.net/array_filter Commented Jan 11, 2022 at 13:14
  • 1. Use foreach (just like you did for the "projects" array). 2. array_filter(). N.B. This actually has nothing to do with JSON. You're not filtering JSON. Your data ceased to be JSON after you decoded it. Now it's just a PHP array structure, so you should search online for solutions which involve filtering PHP arrays. I'll update the question for you. Commented Jan 11, 2022 at 13:22
  • As for the price is that possible to use suggested or similar function from this post and put it before json_decode just to cut json callback? Commented Jan 11, 2022 at 13:35
  • $project['platform_types']['android_phone'] doesn't match the JSON. 'android_phone' is a value, not an array key. Commented Jan 11, 2022 at 14:27
  • @TorbjörnStabo, thank you for explaining. I realized my mistake Commented Jan 11, 2022 at 15:05

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

12 Comments

where in my code should I insert $filtered = array_filter(...)?
I updated the second answer check it
many thanks! It's awesome! What if I would like to combine two or more filters (user's filters to show only what they need)? Is that better way to use functions? Any example to use filters with functions?
I'd say calling array_filter() is a good example of using filters with functions ;). You can probably run the first filter on the unfiltered data, and then run the second filter on the result from the first operation. Or, build a filter function that implements both filters simultaneously. But that tends to be less maintainable in the long run.
you can use the approach that @TorbjörnStabo suggested should work like a charm
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.