There is such a code, $ json is an array of data, the server's response can be different, so you need to dynamically form what to address.

I read this section http://php.net/manual/ru/language.variables.variable.php and I can not refer to the zero element.

$num_hist = count($json->list[0]->Item->HistoryItemList); 

How generally to save the given line list[0]->Item->HistoryItemList then to receive the data having generated $json->... ?

That is, it is required like such an array from which then to assemble a string to get data from $ json

 $arr = [ 'list[0]', 'Item', 'HistoryItemList', ]; 

    1 answer 1

    Variables-variables in this case will not help. You can use eval () (collect the property path in a line of code and execute), but I would suggest another option:

    The second argument of the json_decode () function allows you to represent JSON objects as associative arrays. This will allow the use of a single notation (through square brackets) for any elements.

     $data = json_decode($json, true); // [ // 'list' => [ // 0 => [ // 'Item' => [ // 'HistoryItemList' => 123 // ] // ] // ] // ]; 

    Then you can go through the elements of the path and extract the desired value:

     $path = ['list', 0, 'Item', 'HistoryItemList']; $next = $data; foreach ($path as $element) { $next = $next[$element]; } echo $next; // 123 
    • exactly! supersenx. - Jean-Claude