json_decode and arrays

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #15, on May 31st, 2013, 03:11 AM »
Quote from Arantor 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.
Code: [Select]
    removeAttr($data);

    function removeAttr(&$array) {
        if (isset($array['@attr']['nowplaying']))
            unset($array['WHAT']);
        foreach ($array as &$value) {
            if (is_array($value)) {
                removeAttr($value);
            }
        }
    }

What should I type instead of 'WHAT'?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #16, on May 31st, 2013, 03:15 AM »
Um... unset($array['@attr']); ?

I presume that's what you're trying to do...
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 #17, on May 31st, 2013, 03:34 AM »
Code: [Select]
$data = $data['recenttracks'];
$tracks=$data['track'];

    foreach ($tracks as $index=>$track) {
        if (isset($track['@attr'])) {
            unset($tracks[$index]);
        }
    }

foreach ($tracks as $track) {
$artist = $track['artist']['#text'];
$title = $track['name'];
$url = $track['url'];
echo '<li><a href="', $url, '" title="', $title, '">', $artist, ' - ', $title, '</li></a>'; }

This did the trick :)
Re: json_decode and arrays
« Reply #18, on June 3rd, 2013, 08:25 PM »
What's wrong with this foreach now? :(

Code: [Select]
<ul>
<?php
$json_dailymile "http://api.dailymile.com/people/multiforme/entries.json";
$cache_dailymile '/var/www/multiformeingegno.it/htdocs/wp-content/themes/multiformeingegno/dailymile_json/'.sha1($json_dailymile).'.json';

    if(
file_exists($cache_dailymile) && filemtime($cache_dailymile) > time() - 1000){
        
// if a cache file newer than 1000 seconds exist, use it
        
$data_dailymile json_decode(file_get_contents($cache_dailymile), true);
    } else {
        
$data_dailymile json_decode(file_get_contents($json_dailymile), true);
        
file_put_contents($cache_dailymile,json_encode($data_dailymile));
    }

$data_dailymile $data_dailymile['entries'];

foreach ($data_dailymile['entries'] as $run) {
$distance $run['workout']['distance']['value'];
$units $run['workout']['distance']['units'];
$duration $run['workout']['duration'];
$url $run['url'];
echo '<li><a href="'$url'">'$distance' '$units' in '$duration'</li></a>'; }
?>

</ul>

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #20, on June 3rd, 2013, 08:32 PM »
Quote from Arantor on June 3rd, 2013, 08:27 PM
I don't know, what is wrong with it?
Code: [Select]
2013/06/03 20:29:40 [error] 6002#0: *55092 FastCGI sent in stderr: "PHP message: PHP Warning:  file_put_contents(/BLAHBLAH/dailymile_json/7c51bf1c050135a03bde30dda21a977a2e0ed5c8.json): failed to open stream: Permission denied in /BLAHBLAH/test.php on line 11
PHP message: PHP Notice:  Undefined index: entries in /BLAHBLAH/test.php on line 16
PHP message: PHP Warning:  Invalid argument supplied for foreach() in /BLAHBLAH/test.php on line 16" while reading response header from upstream, client: XXXXX, server: XXXXX, request: "GET /BLAHBLAH/test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "BLAHBLAH"

Posted: June 3rd, 2013, 08:29 PM

Oh, I probably need OAuth :(

It's strange though.. I can access the url properly via browser. They blocked GET requests..?

http://api.dailymile.com/people/multiforme/entries.json
Posted: June 3rd, 2013, 08:30 PM

I'm getting a weird bug editing this post. Strange
Code: [Select]
[error][/error]
tags appear.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #21, on June 3rd, 2013, 08:34 PM »
The cache script doesn't have permission to write to wherever it's trying to write to.
Quote
2013/06/03 20:29:40 error 6002#0: *55092 FastCGI sent in stderr: "PHP message: PHP Warning:  file_put_contents(/var/www/multiformeingegno.it/htdocs/wp-content/themes/multiformeingegno/dailymile_json/7c51bf1c050135a03bde30dda21a977a2e0ed5c8.json): failed to open stream: Permission denied in /var/www/multiformeingegno.it/htdocs/wp-content/themes/multiformeingegno/runs.php on line 11
OT: The tags you're seeing are because the tag closing tool is looking to match bbcode but the opening 'error' bbcode doesn't have a closing tag.

Also, I'm seeing a weird %NEW% indicator in the list of posts here (/do/post2/ showing the post history with an unread post)

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #22, on June 3rd, 2013, 08:56 PM »
You're right as always. I gave proper permissions to the dir and now the file is created.
I still get this though:

Code: [Select]
2013/06/03 20:53:23 [error] 6003#0: *56208 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: entries in /BLAHBLAH/runs.php on line 16
PHP message: PHP Warning:  Invalid argument supplied for foreach() in /BLAHBLAH/runs.php on line 16" while reading response header from upstream, client: XXXXX, server: BLAHBLAH request: "GET /BLAHBLAH/runs.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "BLAHBLAH"
Posted: June 3rd, 2013, 08:55 PM

Solved. It was foreach ($data_dailymile as $run), not foreach ($data_dailymile['entries'] as $run)
Re: json_decode and arrays
« Reply #23, on June 3rd, 2013, 09:09 PM »
I have $date = $run['at'];, which gives me 2013-06-03T16:52:24Z. How can I manipolate it to get for example "d M Y, H:i" ?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

MultiformeIngegno

  • Posts: 1,337

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #26, on June 3rd, 2013, 09:25 PM »
I wasn't sure that strtotime would parse that format you have ;)

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #27, on June 3rd, 2013, 09:30 PM »Last edited on June 3rd, 2013, 09:49 PM
strtotime gives me 03 Jun 2013, 18:52. DateTime gives me 03 Jun 2013, 16:52 (2 hours difference).
One takes server time, the other not?
Posted: June 3rd, 2013, 09:28 PM

I came back to home page after posting and the NEW icon showed up for this topic..

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: json_decode and arrays
« Reply #28, on June 3rd, 2013, 09:33 PM »
One takes into account server time, the other does not...
Quote
I came back to home page after posting and the NEW icon showed up for this topic..
Known bug.

MultiformeIngegno

  • Posts: 1,337