V
V
Vlad Lisovets2019-06-12 13:19:31
PHP
Vlad Lisovets, 2019-06-12 13:19:31

How to get data from JSON in PHP if the object name starts with a number?

Hello colleagues. I apologize right away if I don’t express myself correctly regarding json, because little known to him.
The point is this. There is a Wordpress API that returns data in json.
There is php that accesses the server via curl and receives data in json.
I am trying to output this data, however the json object name starts with a number. How to be?
This is what I ask

$googleApiUrl = "https://clivir.ru/wp-json/wp/v2/posts";


$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);

curl_close($ch);
$data = json_decode($response);
$currentTime = time();

This is what I want to display
$title = ucwords($data->0[0](название объекта)->title->rendered) . "\n";

How to display data in Php from json if the object name is a number?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pushkarev, 2019-06-12
@bokovua

$endpoint = 'https://clivir.ru/wp-json/wp/v2/posts';
$data = json_decode(file_get_contents($endpoint), true);

foreach ($data as $datum) {
    $title = $datum['title']['rendered'];
    $content = $datum['content']['rendered'];

    // ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question