JSON output modifications

I have the following code, I need to only get [list] from JSON response ... not everything

<?php


error_reporting(E_ALL);

$urls = array(
    'https://api.dailymotion.com/user/cricshare/videos?page=1&limit=100',
    'https://api.dailymotion.com/user/cricshare/videos?page=2&limit=100',
    'https://api.dailymotion.com/user/cricshare/videos?page=3&limit=100'
);

$sets = array();
foreach ($urls as $url) {
    $json   = file_get_contents($url);
    $data   = json_decode($json);
    $sets[] = $data;
}

$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

?>

Open in new window


Here is the output:

http://joomclan.com/json2db/json_read.php

Open in new window

LVL 5
CalmSoulAsked:
Who is Participating?
 
Ray PaseurCommented:
This does not seem very efficient, but it appears to work.  You get a merged array of the lists of objects.

<?php // demo/temp_calmsoul.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28634264.html
 */
error_reporting(E_ALL);
echo '<pre>';

$urls = array(
    'https://api.dailymotion.com/user/cricshare/videos?page=1&limit=100',
    'https://api.dailymotion.com/user/cricshare/videos?page=2&limit=100',
    'https://api.dailymotion.com/user/cricshare/videos?page=3&limit=100',
);

$sets = array();
foreach ($urls as $url) {
    $json   = file_get_contents($url);
    $sets[] = json_decode($json);
}
$new = json_encode($sets);
$obj = json_decode($new);

$lists = [];
foreach ($obj as $unit_response)
{
    $lists = array_merge($lists, $unit_response->list);
}
$out = json_encode($lists, JSON_PRETTY_PRINT);

$cnt = count($lists);
echo PHP_EOL . "THERE ARE $cnt ELEMENTS IN THIS ARRAY";
print_r($out);

Open in new window

0
 
CalmSoulAuthor Commented:
Thanks - that worked
0
 
Ray PaseurCommented:
Thanks for the points - it's a great question, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.