Jump to content

Trying to echo data 3 levels deep into json architecture


mosb3rgler

Recommended Posts

good evening folks, i have a question regarding the foreach syntax when needing to access something say 3 levels deep into the json architecture here's the example I'm trying to accomplish but when echoing it's not returning the value i need from 3 levels deep. I believe i just don't understand the correct syntax so here's the example any help would he sincerely appreciated.
Link to comment
Share on other sites

Thank you very much for taking your valuable free time to answer me. I believe i understand your example in relationship to what i provided. I'll give it a try after work and ill report back. You guys are nice and helpful here I'll continue to frequent.

Mosb3rgler

Edited by mosb3rgler
grammar
Link to comment
Share on other sites

Hey i just tested what you suggested. and it actually wasn't quite what i meant, so yes i need 3 levels deep, however its within a foreach loop so i wont know the expected values, i just know the container its and i need to retrieve whats inside of each instance of it. it works for the values which are 2 levels deep just fine, but the 1 i need that i cant get is 3 levels deep. here's what i tried. and if you could direct me on how to proceed it would be sincerely appreciated.

Heres the php file im working on:

https://pastebin.com/e3AgH6xL

 

The only value still needing to be scraped is the stream_id which is 3 levels deep in the json:

 

$stream_id = $json['data']['live_event']['stream_list']['stream_id'];

im not using the above syntax just trying to provide a quick example of the 3 levels for your reference in responding in a timely manor. Thanks in advance for anyone who has the time to answer and correct my mistake.

Link to comment
Share on other sites

Does this example help?

$json = '[{"a1":{"b":{"streamname":"bbbbbbbbbbbbbb","stream_id":123},"c":{"streamname":"ccccccc","stream_id":124}},"a2":{"d":{"streamname":"ddddddddddddd","stream_id":125},"e":{"streamname":"eeeeeeeeeeeee","stream_id":126}}},{"b1":{"b":{"streamname":"bbbbbbbbbbbbbb","stream_id":127},"c":{"streamname":"ccccccc","stream_id":128}},"b2":{"d":{"streamname":"ddddddddddddd","stream_id":129},"e":{"streamname":"eeeeeeeeeeeee","stream_id":130}}}]';

$data = json_decode($json, true);

foreach ($data as $k1 => $data1) {
    foreach ($data1 as $k2 => $data2) {
        foreach ($data2 as $k3 => $data3) {
            echo "$k1 / $k2 / $k3 / {$data3['stream_id']} <br>";
        }
    }
} 

gives

0 / a1 / b / 123 
0 / a1 / c / 124 
0 / a2 / d / 125 
0 / a2 / e / 126 
1 / b1 / b / 127 
1 / b1 / c / 128 
1 / b2 / d / 129 
1 / b2 / e / 130 

 

Link to comment
Share on other sites

This is what I was looking for:

 

$jsondata = file_get_contents("https://kalebdf.com/flo/api.php?url=events%2Fnow-and-upcoming%3Flimit%3D70%26tz%3DAmerica%2FNew_York%26live_only%3D1%26site_id%3D1%252C2%252C3%252C4%252C5%252C6%252C7%252C8%252C9%252C10%252C11%252C12%252C13%252C14%252C15%252C16%252C17%252C18%252C19%252C20%252C21%252C22%252C23%252C24%252C25%252C26%252C27%252C28%252C29%252C30%252C31%252C32%252C33%252C34%252C35%252C36%252C37%252C38%252C39%252C41%252C43");

$json = json_decode($jsondata, true);

var_dump($json['data']);

 

Now that we can see your source json, this is obviously a huge result.  I'm not sure that turning this into a gigantic array is helpful because it's such a large json data structure with many keys.

However, in general you can determine what is going to happen based on whether the json has an array structure ie [ ] or a property  "somename": "some_value"

The original json has many of these properties.  

If something is inside a json array, then it will be turned into a numerically indexed array:   $var[0], $var[1] etc.   Unless you know which specific array element you need, typically you're going to foreach() loop through them.

If the original json is a property then the property name is going to be turned into an array key, with the associated value stored in the array element.  That value could be a nested object:  { }, a single value, or an array.  And in each case there is possible nesting.

If you understand the basics of this, you should be able to look at the original json and figure out how it will be converted.

In your case, when we var_dump($json['data']) the top level is revealed to be a giant numerically indexed array:

Quote

array(70) {

  [0]=>

You are foreach()ing through this array.  So to get an idea of what just one looks like, it's helpful to var_dump the first one:

var_dump($json['data'][0]);

This shows the nested structure for each array element you'll get when you foreach through the outer structure:

 

array(41) {
  ["id"]=>
  int(6251309)
  ["should_show_read_more"]=>
  bool(true)
  ["ad_set_code"]=>
  NULL
  ["slug_uri"]=>
  string(33) "/events/6251309-2019-giro-ditalia"
  ["title"]=>
  string(18) "2019 Giro d'Italia"
  ["short_title"]=>
  string(13) "Giro d'Italia"
  ["seo_description"]=>
  string(122) "Tune in to FloBikes for complete editorial coverage and to watch every stage of the 2019 Giro d'Italia live and on demand!"
  ["author"]=>
  array(10) {
    ["profile_picture_url_small"]=>
    string(86) "https://res.cloudinary.com/diznifoln/image/upload/w_100,h_100/oz7rkl8ihbds5yb20ujn.png"
    ["profile_picture_url_medium"]=>
    string(86) "https://res.cloudinary.com/diznifoln/image/upload/w_125,h_125/oz7rkl8ihbds5yb20ujn.png"
    ["profile_picture_url_large"]=>
    string(86) "https://res.cloudinary.com/diznifoln/image/upload/w_150,h_150/oz7rkl8ihbds5yb20ujn.png"
    ["id"]=>
    int(1625830)
    ["username"]=>
    string(10) "JoeBatts75"
    ["first_name"]=>
    string(3) "Joe"
    ["last_name"]=>
    string(9) "Battaglia"
    ["gender"]=>
    string(1) "m"
    ["created_at"]=>
    string(24) "2014-04-10T14:03:26+0000"
    ["modified_at"]=>
    string(24) "2019-02-04T16:24:23+0000"
  }
  ["asset"]=>
  array(14) {
    ["id"]=>
    int(6456733)
    ["title"]=>
    string(53) "El-Giro-de-Italia-2018-comenzará-en-Jerusalén.jpg"
    ["description"]=>
    NULL
    ["credit"]=>
    NULL
    ["source"]=>
    string(71) "//flo30-assets-prod.s3.amazonaws.com/uploads/FloBikes/5ba8fda8b17eb.png"
    ["site"]=>
    array(13) {
      ["domain"]=>
      string(16) "www.flobikes.com"
      ["id"]=>
      int(37)
      ["name"]=>
      string(8) "FloBikes"
      ["code"]=>
      string(8) "flobikes"
      ["host"]=>
      string(23) "http://www.flobikes.com"
      ["active"]=>
      bool(true)
      ["type"]=>
      string(4) "site"
      ["version"]=>
      int(3)
      ["color"]=>
      string(7) "#FF8200"
      ["hero_image"]=>
      string(86) "https://d6fm3yzmawlcs.cloudfront.net/mobileVerticalBackground/mobile_bg_flocycling.jpg"
      ["modified_at"]=>
      NULL
      ["show_on_mobile"]=>
      bool(true)
      ["sport_name"]=>
      string(7) "Cycling"
    }
    ["url"]=>
    string(55) "https://d2779tscntxxsw.cloudfront.net/5ba8fda8b17eb.png"
    ["library"]=>
    bool(true)
    ["status_code"]=>
    int(200)
    ["path"]=>
    string(77) "https://flo30-assets-prod.s3.amazonaws.com/uploads/FloBikes/5ba8fda8b17eb.png"
    ["copied"]=>
    bool(true)
    ["duplicated"]=>
    bool(false)
    ["created_at"]=>
    string(24) "2018-09-24T15:07:21+0000"
    ["modified_at"]=>
    string(24) "2018-10-13T08:09:22+0000"
  }
  ["logo"]=>
  NULL
  ["slug"]=>
  string(17) "2019-giro-ditalia"
  ["publish_start_date"]=>
  string(24) "2018-09-24T15:26:00+0000"
  ["publish_end_date"]=>
  NULL
  ["status"]=>
  int(1)
  ["status_text"]=>
  string(18) "Active - Published"
  ["status_color"]=>
  string(7) "#CDEB8B"
  ["premium"]=>
  bool(false)
  ["node"]=>
  array(13) {
    ["categories"]=>
    array(3) {
      [0]=>
      array(5) {
        ["id"]=>
        int(605)
        ["name"]=>
        string(4) "Road"
        ["parent"]=>
        array(5) {
          ["id"]=>
          int(602)
          ["name"]=>
          string(10) "Discipline"
          ["parent"]=>
          NULL
          ["created_at"]=>
          string(24) "2018-02-02T17:47:21+0000"
          ["modified_at"]=>
          NULL
        }
        ["created_at"]=>
        string(24) "2018-02-02T17:47:34+0000"
        ["modified_at"]=>
        NULL
      }
      [1]=>
      array(5) {
        ["id"]=>
        int(612)
        ["name"]=>
        string(12) "Professional"
        ["parent"]=>
        array(5) {
          ["id"]=>
          int(603)
          ["name"]=>
          string(11) "Rider Level"
          ["parent"]=>
          NULL
          ["created_at"]=>
          string(24) "2018-02-02T17:47:21+0000"
          ["modified_at"]=>
          NULL
        }
        ["created_at"]=>
        string(24) "2018-02-02T17:47:53+0000"
        ["modified_at"]=>
        NULL
      }
      [2]=>
      array(5) {
        ["id"]=>
        int(621)
        ["name"]=>
        string(8) "UCI 2.WT"
        ["parent"]=>
        array(5) {
          ["id"]=>
          int(604)
          ["name"]=>
          string(10) "Race Level"
          ["parent"]=>
          NULL
          ["created_at"]=>
          string(24) "2018-02-02T17:47:21+0000"
          ["modified_at"]=>
          NULL
        }
        ["created_at"]=>
        string(24) "2018-02-02T17:48:48+0000"
        ["modified_at"]=>
        NULL
      }
    }
    ["primary_event_association"]=>
    NULL
    ["primary_event_or_series_association"]=>
    NULL
    ["people"]=>
    array(0) {
    }
    ["teams"]=>
    array(0) {
    }
    ["id"]=>
    int(6251309)
    ["current_revision"]=>
    NULL
    ["site"]=>
    array(13) {
      ["domain"]=>
      string(16) "www.flobikes.com"
      ["id"]=>
      int(37)
      ["name"]=>
      string(8) "FloBikes"
      ["code"]=>
      string(8) "flobikes"
      ["host"]=>
      string(23) "http://www.flobikes.com"
      ["active"]=>
      bool(true)
      ["type"]=>
      string(4) "site"
      ["version"]=>
      int(3)
      ["color"]=>
      string(7) "#FF8200"
      ["hero_image"]=>
      string(86) "https://d6fm3yzmawlcs.cloudfront.net/mobileVerticalBackground/mobile_bg_flocycling.jpg"
      ["modified_at"]=>
      NULL
      ["show_on_mobile"]=>
      bool(true)
      ["sport_name"]=>
      string(7) "Cycling"
    }
    ["view_count"]=>
    int(100740)
    ["legacy_id"]=>
    NULL
    ["search_indexed_at"]=>
    string(24) "2019-05-22T12:13:40+0000"
    ["created_at"]=>
    string(24) "2018-09-24T15:27:50+0000"
    ["modified_at"]=>
    string(24) "2019-05-22T12:13:40+0000"
  }
  ["enable_interstitial_ad"]=>
  bool(true)
  ["enable_pre_roll_ads"]=>
  bool(true)
  ["shareable_link"]=>
  string(57) "https://www.flobikes.com/events/6251309-2019-giro-ditalia"
  ["created_at"]=>
  string(24) "2018-09-24T15:27:50+0000"
  ["modified_at"]=>
  string(24) "2019-05-22T12:04:37+0000"
  ["has_schedule"]=>
  bool(true)
  ["preview_text"]=>
  string(217) "<p>The 2019 Giro d’Italia is a UCI World Tour event. Tune in to FloBikes for a complete editorial coverage and to watch every stage of the 2019 Giro d'Italia live and on demand, available in the U.S. and Canada.</p>"
  ["description"]=>
  string(225) "<p>The 2019 Giro d&rsquo;Italia is a UCI World Tour event. Tune in to FloBikes for a complete editorial coverage and to watch every stage of the 2019 Giro d&#39;Italia live and on demand, available in the U.S. and Canada.</p>"
  ["city"]=>
  string(7) "Bologna"
  ["region"]=>
  NULL
  ["country"]=>
  string(2) "IT"
  ["venue"]=>
  NULL
  ["start_date"]=>
  string(10) "2019-05-11"
  ["end_date"]=>
  string(10) "2019-06-02"
  ["schedule_tab_label"]=>
  string(8) "Schedule"
  ["participant_tab_label"]=>
  string(7) "Entries"
  ["live_updates_tab_label"]=>
  string(12) "Live Updates"
  ["info_tab_label"]=>
  string(4) "Info"
  ["live_event"]=>
  array(33) {
    ["event_ids"]=>
    array(1) {
      [0]=>
      int(6251309)
    }
    ["start_at"]=>
    string(20) "2019-05-22T11:05:00Z"
    ["end_at"]=>
    string(20) "2019-06-03T03:59:59Z"
    ["id"]=>
    int(8778)
    ["title"]=>
    string(18) "2019 Giro d'Italia"
    ["short_title"]=>
    string(13) "Giro D'Italia"
    ["stream1"]=>
    string(26) "cyp8778_2019_giro_d_italia"
    ["stream2"]=>
    string(26) "cyp8778_2019_giro_d_italia"
    ["stream_list"]=>
    array(2) {
      [0]=>
      array(7) {
        ["stream_id"]=>
        int(18633)
        ["stream_name"]=>
        string(18) "2019 Giro d'Italia"
        ["stream_code"]=>
        string(26) "cyp8778_2019_giro_d_italia"
        ["stream_active"]=>
        bool(true)
        ["stream_hls"]=>
        string(68) "http://flocasts-hls.videocdn.scaleengine.net/flocasts-florigins/play"
        ["stream_type"]=>
        string(8) "multiple"
        ["stream_mode"]=>
        string(6) "normal"
      }
      [1]=>
      array(7) {
        ["stream_id"]=>
        int(25651)
        ["stream_name"]=>
        string(13) "Giro d'Italia"
        ["stream_code"]=>
        string(21) "cyp8778_giro_d_italia"
        ["stream_active"]=>
        bool(false)
        ["stream_hls"]=>
        string(68) "http://flocasts-hls.videocdn.scaleengine.net/flocasts-florigins/play"
        ["stream_type"]=>
        string(8) "multiple"
        ["stream_mode"]=>
        string(6) "normal"
      }
    }
    ["slug"]=>
    string(18) "2019-giro-d-italia"
    ["description"]=>
    string(79) "<p>
	 <span style="background-color: initial;">2019 Giro d'Italia</span>
</p>"
    ["premium"]=>
    bool(true)
    ["ppv"]=>
    bool(false)
    ["disable_bumping"]=>
    bool(false)
    ["disable_login"]=>
    bool(false)
    ["enable_ott"]=>
    bool(true)
    ["status"]=>
    string(4) "LIVE"
    ["status_message"]=>
    NULL
    ["type"]=>
    string(6) "STREAM"
    ["start_date"]=>
    string(10) "2019-05-22"
    ["end_date"]=>
    string(10) "2019-06-02"
    ["start_time"]=>
    string(8) "07:05:00"
    ["end_time"]=>
    string(8) "23:59:59"
    ["timezone"]=>
    string(16) "America/New_York"
    ["start_date_time"]=>
    string(24) "2019-05-22T11:05:00+0000"
    ["end_date_time"]=>
    string(24) "2019-06-03T03:59:59+0000"
    ["start_at_next"]=>
    string(24) "2019-05-22T11:05:00+0000"
    ["background_url"]=>
    NULL
    ["live_event_url"]=>
    string(34) "https://www.flobikes.com/live/8778"
    ["player_version"]=>
    string(3) "3.0"
    ["dvr"]=>
    bool(false)
    ["created_at"]=>
    string(24) "2019-05-22T12:04:56+0000"
    ["modified_at"]=>
    string(24) "2019-05-22T12:04:56+0000"
  }
  ["watchable"]=>
  bool(true)
  ["replay_video_count"]=>
  int(10)
  ["replay_preview_vod"]=>
  NULL
  ["type"]=>
  string(5) "event"
}

 

Sometimes it's useful in such a large structure just to see the list of top level array keys.   PHP has a helpful array_keys() function for this.  Also, print_r has a slightly different output format from var_dump that omits the variable types.  Just to get some clarity, I ran this:

$json = json_decode($jsondata, true);
$t = array_keys($json['data'][0]);
sort($t);
print_r($t);

And got this output:

Array
(
    [0] => ad_set_code
    [1] => asset
    [2] => author
    [3] => city
    [4] => country
    [5] => created_at
    [6] => description
    [7] => enable_interstitial_ad
    [8] => enable_pre_roll_ads
    [9] => end_date
    [10] => has_schedule
    [11] => id
    [12] => info_tab_label
    [13] => live_event
    [14] => live_updates_tab_label
    [15] => logo
    [16] => modified_at
    [17] => node
    [18] => participant_tab_label
    [19] => premium
    [20] => preview_text
    [21] => publish_end_date
    [22] => publish_start_date
    [23] => region
    [24] => replay_preview_vod
    [25] => replay_video_count
    [26] => schedule_tab_label
    [27] => seo_description
    [28] => shareable_link
    [29] => short_title
    [30] => should_show_read_more
    [31] => slug
    [32] => slug_uri
    [33] => start_date
    [34] => status
    [35] => status_color
    [36] => status_text
    [37] => title
    [38] => type
    [39] => venue
    [40] => watchable
)

 

You had determined previously that the 2 top level elements you were interested in were 'title' and 'live_event'.   'title' is a single value, however live_event is another nested structure.

To figure out what that looks like:

var_dump($json['data'][0]['live_event']);

And you get:

array(33) {
  ["event_ids"]=>
  array(1) {
    [0]=>
    int(6251309)
  }
  ["start_at"]=>
  string(20) "2019-05-22T11:05:00Z"
  ["end_at"]=>
  string(20) "2019-06-03T03:59:59Z"
  ["id"]=>
  int(8778)
  ["title"]=>
  string(18) "2019 Giro d'Italia"
  ["short_title"]=>
  string(13) "Giro D'Italia"
  ["stream1"]=>
  string(26) "cyp8778_2019_giro_d_italia"
  ["stream2"]=>
  string(26) "cyp8778_2019_giro_d_italia"
  ["stream_list"]=>
  array(2) {
    [0]=>
    array(7) {
      ["stream_id"]=>
      int(18633)
      ["stream_name"]=>
      string(18) "2019 Giro d'Italia"
      ["stream_code"]=>
      string(26) "cyp8778_2019_giro_d_italia"
      ["stream_active"]=>
      bool(true)
      ["stream_hls"]=>
      string(68) "http://flocasts-hls.videocdn.scaleengine.net/flocasts-florigins/play"
      ["stream_type"]=>
      string(8) "multiple"
      ["stream_mode"]=>
      string(6) "normal"
    }
    [1]=>
    array(7) {
      ["stream_id"]=>
      int(25651)
      ["stream_name"]=>
      string(13) "Giro d'Italia"
      ["stream_code"]=>
      string(21) "cyp8778_giro_d_italia"
      ["stream_active"]=>
      bool(false)
      ["stream_hls"]=>
      string(68) "http://flocasts-hls.videocdn.scaleengine.net/flocasts-florigins/play"
      ["stream_type"]=>
      string(8) "multiple"
      ["stream_mode"]=>
      string(6) "normal"
    }
  }
  ["slug"]=>
  string(18) "2019-giro-d-italia"
  ["description"]=>
  string(79) "<p>
	 <span style="background-color: initial;">2019 Giro d'Italia</span>
</p>"
  ["premium"]=>
  bool(true)
  ["ppv"]=>
  bool(false)
  ["disable_bumping"]=>
  bool(false)
  ["disable_login"]=>
  bool(false)
  ["enable_ott"]=>
  bool(true)
  ["status"]=>
  string(4) "LIVE"
  ["status_message"]=>
  NULL
  ["type"]=>
  string(6) "STREAM"
  ["start_date"]=>
  string(10) "2019-05-22"
  ["end_date"]=>
  string(10) "2019-06-02"
  ["start_time"]=>
  string(8) "07:05:00"
  ["end_time"]=>
  string(8) "23:59:59"
  ["timezone"]=>
  string(16) "America/New_York"
  ["start_date_time"]=>
  string(24) "2019-05-22T11:05:00+0000"
  ["end_date_time"]=>
  string(24) "2019-06-03T03:59:59+0000"
  ["start_at_next"]=>
  string(24) "2019-05-22T11:05:00+0000"
  ["background_url"]=>
  NULL
  ["live_event_url"]=>
  string(34) "https://www.flobikes.com/live/8778"
  ["player_version"]=>
  string(3) "3.0"
  ["dvr"]=>
  bool(false)
  ["created_at"]=>
  string(24) "2019-05-22T12:04:56+0000"
  ["modified_at"]=>
  string(24) "2019-05-22T12:04:56+0000"
}

 

So finally we can see, that 'stream_list' is the key for an array of streams.  

 

Quote

  ["stream_list"]=>
  array(2) {
    [0]=>

Being that this is a numerically indexed array of 1..n items, you need to do a nested foreach() to iterate through the internal array elements.  You've stated that you are only interested in the 'stream_id' element.

 

So hopefully at this point, you can understand that you have an outer foreach() through the data elements, and then the requirement to do an inner foreach to get the individual stream_id elements from stream_list.

<?php
  
$jsondata = file_get_contents("https://kalebdf.com/flo/api.php?url=events%2Fnow-and-upcoming%3Flimit%3D70%26tz%3DAmerica%2FNew_York%26live_only%3D1%26site_id%3D1%252C2%252C3%252C4%252C5%252C6%252C7%252C8%252C9%252C10%252C11%252C12%252C13%252C14%252C15%252C16%252C17%252C18%252C19%252C20%252C21%252C22%252C23%252C24%252C25%252C26%252C27%252C28%252C29%252C30%252C31%252C32%252C33%252C34%252C35%252C36%252C37%252C38%252C39%252C41%252C43");

$json = json_decode($jsondata, true);

foreach($json['data'] as $data) {
  echo "Title: {$data['title']}\n";
  echo "Event Time: {$data['live_event']['start_time']}\n";
  echo "Streams:\n";

  foreach($data['live_event']['stream_list'] as $i => $stream) {
    $i++;
    echo "  $i. {$stream['stream_id']}\n";
  }
  echo "\n";

}

Output: 

Title: 2019 Giro d'Italia
Event Time: 07:05:00
Streams:
  1. 18633
  2. 25651

Title: 2019 Japan vs United States | Women's VNL
Event Time: 08:45:00
Streams:
  1. 24691

Title: 2019 Serbia vs Korea | Women's VNL
Event Time: 09:45:00
Streams:
  1. 24692

Title: 2019 Thailand vs Italy | Women's VNL
Event Time: 10:15:00
Streams:
  1. 24695

Title: 2019 Brose Bamberg vs Rasta Vechta | easyCredit BBL
Event Time: 12:00:00
Streams:
  1. 25885

Title: 2019 Bulgaria vs Belgium | Women's VNL
Event Time: 12:15:00
Streams:
  1. 24697

Title: 2019 Netherlands vs Turkey | Women's VNL
Event Time: 12:45:00
Streams:
  1. 24698

Title: 2019 Poland vs Germany | Women's VNL
Event Time: 13:15:00
Streams:
  1. 24699

Title: 2019 Kirolbet Baskonia vs Divinas Seguros Joventut | 2018-19 La Liga BB
Event Time: 13:30:00
Streams:
  1. 24555

Title: Here's The Deal
Event Time: 14:30:00
Streams:
  1. 17181

Title: 2019 China vs Russia | Women's VNL
Event Time: 14:45:00
Streams:
  1. 24700

Title: 2019 TSSAA Outdoor Championships
Event Time: 17:00:00
Streams:
  1. 23950

Title: Going Brooklyn: In Studio With Bowling's Big Names
Event Time: 16:00:00
Streams:
  1. 19566

Title: 2019 USAC Sprints at Terre Haute Action Track
Event Time: 17:30:00
Streams:
  1. 20891
  2. 21547
  3. 21850

Title: 2019 Brazil vs Dominican Republic | Women's VNL
Event Time: 17:45:00
Streams:
  1. 24701

Title: 2019 D.C. United vs Real Betis
Event Time: 18:45:00
Streams:
  1. 25324
  2. 25961

Title: 2019 Sassari City Matteo Pellicone Memorial
Event Time: 03:00:00
Streams:
  1. 22004

Title: 2019 Canadian Gymnastics Championships - Artistic
Event Time: 08:30:00
Streams:
  1. 22977

Title: 2019 Belgium vs Japan | Women's VNL
Event Time: 08:45:00
Streams:
  1. 24702

Title: 2019 CAN vs SUI | IIHF World Quarterfinals
Event Time: 09:15:00
Streams:
  1. 25647
  2. 25958

Title: 2019 Netherlands vs Korea | Women's VNL
Event Time: 09:45:00
Streams:
  1. 24703

Title: 2019 Italy vs Germany | Women's VNL
Event Time: 10:15:00
Streams:
  1. 24704

Title: FloTrack TV
Event Time: 12:00:00
Streams:
  1. 24624

Title: 2019 United States vs Bulgaria | Women's VNL
Event Time: 12:15:00
Streams:
  1. 24705

Title: 2019 Turkey vs Serbia | Women's VNL
Event Time: 12:45:00
Streams:
  1. 24706

Title: 2019 Thailand vs Poland | Women's VNL
Event Time: 13:15:00
Streams:
  1. 24707

Title: 2019 CZE vs GER | IIHF World QuarterFinals
Event Time: 13:15:00
Streams:
  1. 25959

Title: 2019 DI NCAA West Preliminary Round
Event Time: 12:00:00
Streams:
  1. 23835
  2. 25953

Title: 2019 DI NCAA East Preliminary Round
Event Time: 15:30:00
Streams:
  1. 23834
  2. 25954

Title: 2019 China vs Dominican Republic | Women's VNL
Event Time: 14:45:00
Streams:
  1. 24708

Title: 2019 MPSSAA Outdoor Championships
Event Time: 16:00:00
Streams:
  1. 24005

Title: 2019 NHSRA Louisiana State Finals: RidePass PRO
Event Time: 15:15:00
Streams:
  1. 25111

etc.

 

  • Great Answer 1
Link to comment
Share on other sites

hey there, thank you so much for your incredibly thoughtful response and examples, i did give a whirl to see how the code would look online and my attempt word wraps all the results jumbled up could you tell me why this might be ?

http://prntscr.com/ns2gch

and also would it be possible to do like this:

1.) 25111

for the stream id's being listed. im not seeing where the numbering is being done and i have a feeling popping the odd ) character without understanding where it can go might break something.

Edited by mosb3rgler
Link to comment
Share on other sites

The reason things are jumbled is because the output was set for command line php.  If you run this as a script via a webserver, then you will be outputting html and you would need to add the desired html.  Just to get a quick look at it, you can add pre tags around it.  Anything else would be a tutorial in html and I have to assume you can do that on your own.

 

echo "<pre>";

foreach($json['data'] as $data) {
  echo "Title: {$data['title']}\n";
  echo "Event Time: {$data['live_event']['start_time']}\n";
  echo "Streams:\n";

  foreach($data['live_event']['stream_list'] as $i => $stream) {
    $i++;
    echo "  $i. {$stream['stream_id']}\n";
  }
  echo "\n";
}
echo "</pre>";

 

As for format of the number, the important thing is that you read my code and figure out why it does what it does.  Then changing the way the number looks should be easy.

As for the date/time, I just used what you were using, but it looks like if you want a Datetime value, you should use $data['live_event']['start_at'] instead of start_time.  

In order to change the format you might need to look into the php DateTime Class.

  • Great Answer 1
Link to comment
Share on other sites

Thank you very much for your time and examples, I'm going to deep dive into your code and examples and see if I can play with that array and dig around to get different responses for practice. and I'll also investigate the datetime class details and see if there's a way to break it down further and make it a more readable format.

Really impressed with your ability to explain in great detail. very glad i accidentally bumped into this place, and I plan to be a long time frequent user of the forums here

  • Like 1
Link to comment
Share on other sites

i was able to edit correctly and get the characters to look how i wanted, i used start_time and start_date to get the version of those i needed, and i even added timezone for clearification further on the automation needs moving forward for reference. So i see how you used $i the i believe its $i++ was that to say count incrementally higher ? and if so is this same thing possible only using a-z intead of 0-9

  • Like 1
Link to comment
Share on other sites

5 hours ago, mosb3rgler said:

i was able to edit correctly and get the characters to look how i wanted, i used start_time and start_date to get the version of those i needed, and i even added timezone for clearification further on the automation needs moving forward for reference. So i see how you used $i the i believe its $i++ was that to say count incrementally higher ? and if so is this same thing possible only using a-z intead of 0-9

Correct.  Because it's a numeric array, when you do foreach($key => $value)  the array key is going to be either an "associative" key  ie some string like 'title', or it will just be numeric.  

Other languages often have completely different data structures for these things like array vs. map, but one of the really nice features of PHP is that they intermix the two.  

Since the array I was foreaching through was numeric, the first element will be 0.  PHP is the same as most other languages (c, c++, javascript, c#, java) in that the first element of an array will be indexed at 0.    ++ is the increment operator, so it just adds 1 to the value. Thus you get things numbered starting at 1.  

If you want a different numbering system, I would actually recommend not using your own numbering at all, but instead emit an html ordered list, and use css to style the list for you.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.