-2

Hello i try to have access to some properties with json_decode but it doesn't work. I would like to have the first userRating and pressRating properties.

$result_json = json_decode($result, true);
echo $result_json['userRating'];

Notice: Undefined index: userRating in /var/www/html/test.php on line 79

var_export gives :

array (
  'feed' => 
  array (
    'page' => 1,
    'count' => 10,
    'results' => 
    array (
      0 => 
      array (
        'type' => 'movie',
        '$' => 10,
      ),
    ),
    'totalResults' => 10,
    'movie' => 
    array (
      0 => 
      array (
        'code' => 27405,
        'originalTitle' => 'Oblivion',
        'productionYear' => 2013,
        'release' => 
        array (
          'releaseDate' => '2013-04-10',
        ),
        'castingShort' => 
        array (
          'directors' => 'Joseph Kosinski',
          'actors' => 'Tom Cruise, Olga Kurylenko, Morgan Freeman, Andrea Riseborough, Nikolaj Coster-Waldau',
        ),
        'statistics' => 
        array (
          'pressRating' => 2.94444,
          'userRating' => 3.62211,
        ),
        'poster' => 
        array (
          'path' => '/medias/nmedia/18/95/15/80/20495053.jpg',
          'href' => 'http://fr.web.img1.acsta.net/medias/nmedia/18/95/15/80/20495053.jpg',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=27405.html',
          ),
        ),
      ),
      1 => 
      array (
        'code' => 220098,
        'originalTitle' => 'Oblivion',
        'productionYear' => 2005,
        'castingShort' => 
        array (
          'directors' => 'Stephen Dwoskin',
          'actors' => 'Ana Benegas, Beatrice Cordua, Stephen Dwoskin, Maggie Jennings',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=220098.html',
          ),
        ),
      ),
      2 => 
      array (
        'code' => 180884,
        'originalTitle' => 'Oblivion',
        'productionYear' => 1994,
        'castingShort' => 
        array (
          'directors' => 'Sam Irvin',
          'actors' => 'Meg Foster, Andrew Divoff, Isaac Hayes, George Takei, Musetta Vander',
        ),
        'statistics' => 
        array (
          'userRating' => 2.86878,
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=180884.html',
          ),
        ),
      ),
      3 => 
      array (
        'code' => 274969,
        'originalTitle' => 'Oblivion Song',
        'productionYear' => 2020,
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=274969.html',
          ),
        ),
      ),
      4 => 
      array (
        'code' => 12880,
        'originalTitle' => 'Living in Oblivion',
        'title' => 'Ca tourne à Manhattan',
        'productionYear' => 1995,
        'release' => 
        array (
          'releaseDate' => '1995-09-13',
        ),
        'castingShort' => 
        array (
          'directors' => 'Tom DiCillo',
          'actors' => 'Steve Buscemi, Catherine Keener, Dermot Mulroney, James LeGros, Danielle von Zerneck',
        ),
        'statistics' => 
        array (
          'userRating' => 3.40096,
        ),
        'poster' => 
        array (
          'path' => '/medias/nmedia/images/affiches/033462.jpg',
          'href' => 'http://fr.web.img1.acsta.net/medias/nmedia/images/affiches/033462.jpg',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=12880.html',
          ),
        ),
      ),
      5 => 
      array (
        'code' => 173081,
        'originalTitle' => 'Sands of Oblivion',
        'title' => 'La Malédiction des sables',
        'productionYear' => 2007,
        'castingShort' => 
        array (
          'directors' => 'David Flores',
          'actors' => 'Morena Baccarin, Adam Baldwin, Victor Webster, George Kennedy, Azie Tesfai',
        ),
        'statistics' => 
        array (
          'userRating' => 2.40935,
        ),
        'poster' => 
        array (
          'path' => '/pictures/210/121/21012170_20130612183620066.jpg',
          'href' => 'http://fr.web.img2.acsta.net/pictures/210/121/21012170_20130612183620066.jpg',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=173081.html',
          ),
        ),
      ),
      6 => 
      array (
        'code' => 47577,
        'originalTitle' => 'Wondrous Oblivion',
        'productionYear' => 2003,
        'castingShort' => 
        array (
          'directors' => 'Paul Morrison',
          'actors' => 'Delroy Lindo, Emily Woof, Carol MacReady, Stanley Townsend, Sam Smith',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=47577.html',
          ),
        ),
      ),
      7 => 
      array (
        'code' => 124080,
        'originalTitle' => 'Oblivion / Moment',
        'productionYear' => 2002,
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=124080.html',
          ),
        ),
      ),
      8 => 
      array (
        'code' => 114857,
        'originalTitle' => 'Phantasm IV: Oblivion',
        'title' => 'Phantasm 4',
        'productionYear' => 1998,
        'castingShort' => 
        array (
          'directors' => 'Don Coscarelli',
          'actors' => 'A. Michael Baldwin, Reggie Bannister, Bill Thornbury, Angus Scrimm, Heidi Marnhout',
        ),
        'statistics' => 
        array (
          'userRating' => 2.82367,
        ),
        'poster' => 
        array (
          'path' => '/medias/nmedia/18/71/35/98/19132495.jpg',
          'href' => 'http://fr.web.img6.acsta.net/medias/nmedia/18/71/35/98/19132495.jpg',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=114857.html',
          ),
        ),
      ),
      9 => 
      array (
        'code' => 258222,
        'originalTitle' => 'Los versos del olvido',
        'title' => 'Les Versets de l\'oubli',
        'productionYear' => 2017,
        'release' => 
        array (
          'releaseDate' => '2018-08-01',
        ),
        'castingShort' => 
        array (
          'directors' => 'Alireza Khatami',
          'actors' => 'Juan Margallo, Tomas del Estal, Manuel Moron, Itziar Aizpuru, Julio Jung',
        ),
        'statistics' => 
        array (
          'pressRating' => 2.33333,
          'userRating' => 3.17865,
        ),
        'poster' => 
        array (
          'path' => '/pictures/18/06/18/17/15/1551602.jpg',
          'href' => 'http://fr.web.img2.acsta.net/pictures/18/06/18/17/15/1551602.jpg',
        ),
        'link' => 
        array (
          0 => 
          array (
            'rel' => 'aco:web',
            'href' => 'http://www.allocine.fr/film/fichefilm_gen_cfilm=258222.html',
          ),
        ),
      ),
    ),
  ),
)

Thank you !

Phil
  • 157,677
  • 23
  • 242
  • 245
Alex
  • 19
  • 3
  • 2
    Can you use please `var_export()` instead of `var_dump()`? So we can copy paste and rebuild the array – Cid Sep 30 '20 at 07:54

1 Answers1

1

When you convert it into an array, check at which level this userRating is present. By looking at your data. It is something like this

feed -> movie(an array) -> statistics -> userRating

Now except movie all are direct attribute, so we can access them directly, but all attributes which comes at a deeper level then movie can only be accessible if we do loop over movie, as it is an array.

So here is the way you can access userRating

$arrData = json_decode($result, true);

foreach($arrData['feed']['movie'] as $movieData)
{
   echo $movieData['statistics']['userRating'];
}
Dark Knight
  • 6,116
  • 1
  • 15
  • 37