json_decode and arrays

MultiformeIngegno

  • Posts: 1,337
json_decode and arrays
« on May 24th, 2013, 05:06 PM »
Code: [Select]
<?php
$json 
file_get_contents('http://pastebin.com/NLHGMapT');
$track_data json_decode($json);
?>

<?php
echo '<ul>';
    foreach (
$track_data as $data)
    {
        if (!empty(
$data)) {
            
$text $data->track[0]->artist->{'#text'};
        }
        echo 
'<li><span>'; echo $text; echo '</span></li>';
    }
echo 
'</ul>';
?>

How can I have $text for every array (track[0], track[1], track[2], track[n]) in respective <li>s?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #1, on May 24th, 2013, 05:11 PM »
First glance says that you need to check !empty($data) then foreach ($data->track as $track) then you can iterate over $track to get artist and whatnot.

Possibly you might find it easier to wade through if you pulled it back from json_decode as an associative array rather than an object.
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #2, on May 25th, 2013, 12:29 PM »
Quote from Arantor on May 24th, 2013, 05:11 PM
First glance says that you need to check !empty($data) then foreach ($data->track as $track) then you can iterate over $track to get artist and whatnot.
Uhm, but before $track_data as $data, $data is not defined..
Quote from Arantor on May 24th, 2013, 05:11 PM
Possibly you might find it easier to wade through if you pulled it back from json_decode as an associative array rather than an object.
Something like this.. ?
Code: [Select]
$json = file_get_contents('http://pastebin.com/NLHGMapT');
$arrayObjects = json_decode($json, true);

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #3, on May 25th, 2013, 03:03 PM »
Um... you do foreach track_data as data, then foreach (data->track as track)...

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #4, on May 30th, 2013, 06:04 PM »
This appears to be working :)

Code: [Select]
<ul>
<?php
$json file_get_contents('http://pastebin.com/NLHGMapT');
$data json_decode($jsontrue);
$data $data['recenttracks'];

foreach ($data['track'] as $track) {

$artist $track['artist']['#text'];
$title $track['name'];
$url $track['url'];
echo '<li><a href="'; echo $url; echo '" title="'; echo $title; echo '">'; echo $artist; echo ' - '; echo $title; echo '</li></a>'; }
?>

</ul>

Can be improved or is it ok?

Nao

  • Dadman with a boy
  • Posts: 16,079
Re: json_decode and arrays
« Reply #5, on May 30th, 2013, 06:26 PM »
Well, apart from the indentation and multiple echos... :^^;:

MultiformeIngegno

  • Posts: 1,337

ethankcvds

  • Good news I finally have a new computer. Bad news I have to RMA the memory got a bad stick of RAM.
  • Posts: 35
Re: json_decode and arrays
« Reply #7, on May 30th, 2013, 07:43 PM »
Quote from MultiformeIngegno on May 30th, 2013, 06:58 PM
Quote from Nao on May 30th, 2013, 06:26 PM
Well, apart from the indentation and multiple echos... :^^;:
I still haven't properly learned the " . " trick to avoid the repetition.. :P
If this is of any help. You can also do it with a ,

Code: [Select]
echo '<a href="'.$url.'">'.$txt.'<a/>';
echo '<a href="',$url,'">',$txt,'<a/>';

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #8, on May 30th, 2013, 07:46 PM »
Uh, thanks. And are they the same in this case (dots and commas)?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #9, on May 30th, 2013, 07:48 PM »
Doing it with a , has other consequences that with a . does not. Specifically, if you get into the realms of doing inline ternary expressions, you need to be careful of operator priority.

echo 'string' . $var > 1 ? 'high' : 'low'; does something different to echo 'string', $var > 1 ? 'high' : 'low';

The reason is that the concatenation will occur before the ternary is evaluated, so you'd have to wrap it in parentheses for it to work properly, i.e. $echo 'string' . ($var > 1 ? 'high' : 'low'); is what's required.


In this particular case it'd be OK:
Code: [Select]
echo '<li><a href="', $url, '" title="', $title, '">', $artist, ' - ', $title, '</li></a>';

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #10, on May 30th, 2013, 08:06 PM »
Thanks for the clarification. :)
I'm trying to add a caching mechanism.. local .json file is created, but "null" is its content :P

Code: [Select]
$json = "http://pastebin.com/NLHGMapT";
$cache_lastfm = 'BLAHBLAH/'.sha1($json).'.json';

    if(file_exists($cache_lastfm) && filemtime($cache_lastfm) > time() - 1000){
        // if a cache file newer than 1000 seconds exists, use it
        $data = json_decode(file_get_contents($cache_lastfm, true));
    } else {
        $data = json_decode($json, true);
        file_put_contents($cache_lastfm,json_encode($data));
    }

What's wrong with this?

Dragooon

  • I can code! Really!
  • polygon.com has to be one of the best sites I've seen recently.
  • Posts: 1,841
Re: json_decode and arrays
« Reply #11, on May 30th, 2013, 10:02 PM »
You're decoding the URL under the else, you have to grab the contents of the URL.
The way it's meant to be

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #12, on May 30th, 2013, 11:10 PM »
Yay, now works :)

Code: [Select]
else {
        $data = json_decode(file_get_contents($json), true);
        file_put_contents($cache_lastfm,json_encode($data));
    }
Re: json_decode and arrays
« Reply #13, on May 31st, 2013, 03:05 AM »
How can I remove an "object" from JSON input if

Code: [Select]
        "@attr":{
           "nowplaying":"true"
        }

... is present in that object? To be clear, the right fragment is how it should be treated :) http://diffchecker.com/c9ha939m

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #14, on May 31st, 2013, 03:08 AM »
If it's an array, unset it like you would any other item. In theory the same should be true for using it as an object but I don't do the whole iterated objects thing if I can help it. The memory use is significantly higher.