0

I use a GET request to access an API. I first filter the returned data like this:

$results = array_filter($body, function($item) {
    if(!isset($item['schedule']['type']))
        return false;
    return $item['schedule']['type'] == "weekly";
});

Condensed version of the returned array:

array (
    6 => 
        array (
            'foo' => 'other fields x 100',
            'title' => 'Episode 1 Title',
            'automatically_title_stream' => false,
            'stream_title' => 'Episode 1 Title',
            'stream_description' => 'Join us each afternoon for real-time market analysis and live futures trading in this interactive live stream event.',
            'rtmp_link' => 'rtmp://rtmp-global.cloud.vimeo.com/live',
            'rtmps_link' => 'rtmps://rtmp-global.cloud.vimeo.com:443/live',
            'stream_key' => '97deb5f6-9bef-4e1c-91f8-d39f72657290',
            'schedule' => 
                array (
                  'type' => 'weekly',
                  'start_time' => NULL,
                  'daily_time' => '19:15:00Z',
                  'scheduled_time' => NULL,
                  'weekdays' => 
                      array (
                        0 => 1,
                        1 => 2,
                        2 => 3,
                        3 => 4,
                        4 => 5,
                     ),
                ),
            ),
    7 => 
          array (
            'foo' => 'other fields x 100',
            'title' => 'Episode 2 Title',
            'automatically_title_stream' => false,
            'stream_title' => 'Bars Closing',
            'stream_description' => 'Join us each afternoon for real-time market analysis and live futures trading in this interactive live stream event.',
            'rtmp_link' => 'rtmp://rtmp-global.cloud.vimeo.com/live',
            'rtmps_link' => 'rtmps://rtmp-global.cloud.vimeo.com:443/live',
            'stream_key' => '97deb5f6-9bef-4e1c-91f8-d39f72657290',
            'schedule' => 
            array (
                'type' => 'weekly',
                'start_time' => NULL,
                'daily_time' => '19:15:00Z',
                'scheduled_time' => NULL,
                'weekdays' => 
                      array (
                        0 => 1,
                        1 => 2,
                        2 => 3,
                        3 => 4,
                        4 => 5,
            ),
        ),
       )
     )

This works in the fact it only returns objects with a ['schedule']['type'] == "weekly". The problem is that the array is HUGE, over 56K lines. I do not need all of it, only certain fields.

I want to filter it again, after I've returned the "weekly" data, and only return "title", "stream_description" and the "weekdays" values. However, I get an empty array when filtering like this:

$allowed  = ['title', 'stream_description', 'weekdays'];
$filtered = array_filter(
    $results,
    fn ($key) => in_array($key, $allowed),
    ARRAY_FILTER_USE_KEY
);

I have other, more complex arrays to filter as well. How do I create a new array from only a specified list of key's and eliminate all the unnecessary data from the array?

Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • `array_filter()` doesn't change the array elements. You'll need to do that in some other code, either before or after you filter. – Barmar Oct 09 '22 at 21:52
  • Damn. I thought that was exactly what array_filter() does, filter the array. How do you eliminate unnecessary data from an array? – Dirty Bird Design Oct 09 '22 at 21:54

2 Answers2

1

You're filtering the main array, but you need to filter the nested arrays. For that you should use array_map() to process each nested array. And some of your allowed keys are nested further down -- this makes it difficult to use a simple array_filter on the keys.

Simplify it to just return the 3 keys you want in code.

$filtered = array_map(function($row) {
    return [
        'title' => $row['title'],
        'stream_description' => $row['stream_description'],
        'weekdays' => $row['schedule']['weekdays']
    ];
}, $results);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Still returns empty arrays: Array ( [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( ) [5] => Array ( ) ) – Dirty Bird Design Oct 09 '22 at 22:03
  • https://ideone.com/5gzJrR shows it returning the `title` and `stream_description'. However, it doesn't return `weekdays` because that's nested inside the `schedule` key. This is only filtering the top-level keys. – Barmar Oct 09 '22 at 22:06
  • Thank you Barmar. Im struggling to communicate what I need, and starting to doubt I know what I need. – Dirty Bird Design Oct 09 '22 at 22:10
  • Im having to condense the returned data because its freaking huge. Too much to paste or save in a pastebin. Im already filtering the returned data for those that contain a weekly schedule. I can then break that out into an array with an object for each day (containing objects related to that day). I will try and get this final step, returning only specified fields, to work – Dirty Bird Design Oct 09 '22 at 22:15
  • I will try and get this to work from here. I don't have a way to share all this code to show the errors. Again, thank you Barmar. – Dirty Bird Design Oct 09 '22 at 22:18
0

I'm old school and like to keep things simple.
I'd just use a foreach()
I think this may be what you were trying to do.

<?php
header("Content-Type: text/plain; UTF-8");
$response = array ( 0 =>  array ( 'foo' => 'other fields x 100', 'title' => 'Episode 2 Title', 'automatically_title_stream' => false, 'stream_title' => 'Bars Closing', 'stream_description' => 'Join us each afternoon for real-time market analysis and live futures trading in this interactive live stream event.', 'rtmp_link' => 'rtmp://rtmp-global.cloud.vimeo.com/live', 'rtmps_link' => 'rtmps://rtmp-global.cloud.vimeo.com:443/live', 'stream_key' => '97deb5f6-9bef-4e1c-91f8-d39f72657290', 'schedule' =>  array ( 'type' => 'weekly', 'start_time' => NULL, 'daily_time' => '19:15:00Z', 'scheduled_time' => NULL, 'weekdays' =>  array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ), ), ), 1 =>  array ( 'foo' => 'other fields x 100', 'title' => 'Episode 1 Title', 'automatically_title_stream' => false, 'stream_title' => 'Episode 1 Title', 'stream_description' => 'Join us each afternoon for real-time market analysis and live futures trading in this interactive live stream event.', 'rtmp_link' => 'rtmp://rtmp-global.cloud.vimeo.com/live', 'rtmps_link' => 'rtmps://rtmp-global.cloud.vimeo.com:443/live', 'stream_key' => '97deb5f6-9bef-4e1c-91f8-d39f72657290', 'schedule' =>  array ( 'type' => 'weekly', 'start_time' => NULL, 'daily_time' => '19:15:00Z', 'scheduled_time' => NULL, 'weekdays' =>  array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ), ), ),)


foreach($response as $array){
  if ($array['schedule']['type'] == 'weekly'){
    $results[] = array('stream_description' => $array['stream_description'],'title' => $array['title'],'weekdays' => $array['schedule']['weekdays']);
  }
}
var_export($results);

The results:

array (
 0 => 
 array (
 'stream_description' => 'Join us each afternoon for real-time market analysis and live futures trading in this interactive live stream event.',
 'title' => 'Episode 2 Title',
 'weekdays' =>  array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ), ),
 
 1 => 
 array (
 'stream_description' => 'Join us each afternoon for real-time market analysis and live futures trading in this interactive live stream event.',
 'title' => 'Episode 1 Title',
 'weekdays' =>  array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ), ),
)
Misunderstood
  • 5,534
  • 1
  • 18
  • 25