如何在PHP中解析JSON对象数组

I am new to php programming. Here in my project I am trying to parse JSON data coming from php web service. Here is the code in web service.

$query = "select * from tableA where ID = 1";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
    $arr= array();
    while ($row = mysql_fetch_assoc($result)) {
        $arr['articles'][] = $row;
    }
    header('Content-type: application/json');
    echo json_encode($arr);
}
else{
    echo "No Names";
}

This is giving me data in this JSON format.

{"articles":[{"ID":"1","Title":"Welcome","Content":"This is the first article."}]}

Now here is my php page code.

<?php
$jfile = file_get_contents('http://localhost/api/get_content.php');
$final_res = json_decode($jfile, true) ;
var_dump( $final_res );
$content = $final_res->articles->Content;
?>

I want to show the content on webpage.

I know code at var_dump( $final_res ); is working. But after that code is wrong. I tried to look at many tutorials to find the solution but didn't find anyone. I don't know where I am wrong.

The second parameter of json_decode determines whether to return the result as an array instead of an object. Since you set it to true your result is an array and not an object.

$content = $final_res['articles'][0]['Content'];

As an alternative answer, if you want to use it as an object, use this code:

$a = '{"articles":[{"ID":"1","Title":"Welcome","Content":"This is the first article."}]}';
$final_res = json_decode($a);

echo '<pre>';
print_r($final_res);
echo '</pre><br>';

Note that I removed the second part (true) from the json_decode

Output:

stdClass Object
(
    [articles] => Array
        (
            [0] => stdClass Object
                (
                    [ID] => 1
                    [Title] => Welcome
                    [Content] => This is the first article.
                )

        )

)

Accessing Content:

echo 'Content: ' . $final_res->articles[0]->Content;

Output:

Content: This is the first article.

Run code