如何在stdClass中获取数组

时间:2017-07-04 08:36:04

标签: php json

我正在尝试用json解码 http://digitsolutions4u.com/apps_services/statuslist.php?catid=1 像这样

<?php 
$json_url = "http://digitsolutions4u.com/apps_services/statuslist.php?catid=1";
$json = file_get_contents($json_url);
$json=str_replace('},

]',"}

]",$json);
$data = json_decode($json);

echo "<pre>";
print_r($data);
echo "</pre>";
?>

我想在解码后获得所有“statusdesc”值。 我得到的结果是这样的

stdClass Object
(
    [posts] => Array
        (
            [0] => stdClass Object
                (
                    [statusid] => 1329
                    [statusdesc] => love86.jpg
                )
)
)

我只想列出页面上可用的所有statusdesc love86.jpg love87.jpg love88.jpg 等等。任何人都可以为我提供解决方案(代码)?我尝试了几个线程,但没有成功。

4 个答案:

答案 0 :(得分:2)

默认情况下json_decode将其解码为对象而不是关联数组。您只需通过箭头(->)表示法访问该对象的成员。要获取每个帖子,您可以使用foreach循环播放它们。

foreach ($data->posts as $post) {
  echo $post->statusdesc;
}

答案 1 :(得分:1)

您需要做的就是告诉json_decode()您希望它将JSON字符串转换为所有数组,而不是将JSONString转换为对象

<?php 
$json_url = "http://digitsolutions4u.com/apps_services/statuslist.php?catid=1";
$json = file_get_contents($json_url);

$data = json_decode($json, true);
// the change ..  ..  ..   ^^^^

echo "<pre>";
print_r($data);
echo "</pre>";
foreach ($json['posts'] as $post) {
    echo $post['statusdesc'] . '<br>';
}
?>

然而,更简单的是处理它并且做到了

<?php 
$json_url = "http://digitsolutions4u.com/apps_services/statuslist.php?catid=1";
$json = file_get_contents($json_url);

$data = json_decode($json);

//echo "<pre>";
//print_r($data);
//echo "</pre>";

foreach ($json->posts as $post) {
    echo $post->statusdesc . '<br>';
}
?>

答案 2 :(得分:0)

json_decode()与第二个参数true一起使用。它将json字符串作为关联数组返回。并像这样使用array_column

<?php

    $response ='{"posts":[{"statusid":"1329","statusdesc":"love86.jpg"},{"statusid":"1328","statusdesc":"love85.jpg"},{"statusid":"1327","statusdesc":"love84.jpg"},{"statusid":"1326","statusdesc":"love83.jpg"},{"statusid":"1325","statusdesc":"love82.jpg"},{"statusid":"1324","statusdesc":"love81.jpg"},{"statusid":"1323","statusdesc":"love80.jpg"},{"statusid":"1322","statusdesc":"love79.jpg"},{"statusid":"1321","statusdesc":"love78.jpg"},{"statusid":"1320","statusdesc":"love77.jpg"},{"statusid":"1319","statusdesc":"love76.jpg"},{"statusid":"1318","statusdesc":"love75.jpg"},{"statusid":"1317","statusdesc":"love74.jpg"},{"statusid":"1316","statusdesc":"love73.jpg"},{"statusid":"1315","statusdesc":"love72.jpg"},{"statusid":"1314","statusdesc":"love71.jpg"},{"statusid":"1313","statusdesc":"love70.jpg"},{"statusid":"1312","statusdesc":"love69.jpg"},{"statusid":"1311","statusdesc":"love68.jpg"},{"statusid":"1310","statusdesc":"love67.jpg"},{"statusid":"1287","statusdesc":"love66.jpg"},{"statusid":"1286","statusdesc":"love65.jpg"},{"statusid":"1285","statusdesc":"love64.jpg"},{"statusid":"1284","statusdesc":"love63.jpg"},{"statusid":"1283","statusdesc":"love62.jpg"},{"statusid":"1282","statusdesc":"love61.jpg"},{"statusid":"1204","statusdesc":"love60.jpg"},{"statusid":"1203","statusdesc":"love59.jpg"},{"statusid":"1202","statusdesc":"love58.jpg"},{"statusid":"1201","statusdesc":"love57.jpg"},{"statusid":"1200","statusdesc":"love56.jpg"},{"statusid":"1199","statusdesc":"love55.jpg"},{"statusid":"1045","statusdesc":"love54.jpg"},{"statusid":"1044","statusdesc":"love53.jpg"},{"statusid":"1043","statusdesc":"love52.jpg"},{"statusid":"1042","statusdesc":"love51.jpg"},{"statusid":"1004","statusdesc":"love50.jpg"},{"statusid":"1003","statusdesc":"love49.jpg"},{"statusid":"1002","statusdesc":"love48.jpg"},{"statusid":"1001","statusdesc":"love47.jpg"},{"statusid":"1000","statusdesc":"love46.jpg"},{"statusid":"239","statusdesc":"love45.jpg"},{"statusid":"238","statusdesc":"love44.jpg"},{"statusid":"237","statusdesc":"love43.jpg"},{"statusid":"236","statusdesc":"love42.jpg"},{"statusid":"235","statusdesc":"love41.jpg"},{"statusid":"234","statusdesc":"love40.jpg"},{"statusid":"233","statusdesc":"love39.jpg"},{"statusid":"232","statusdesc":"love38.jpg"},{"statusid":"231","statusdesc":"love37.jpg"},{"statusid":"230","statusdesc":"love36.jpg"},{"statusid":"229","statusdesc":"love35.jpg"},{"statusid":"228","statusdesc":"love34.jpg"},{"statusid":"227","statusdesc":"love33.jpg"},{"statusid":"226","statusdesc":"love32.jpg"},{"statusid":"31","statusdesc":"love31.jpg"},{"statusid":"30","statusdesc":"love30.jpg"},{"statusid":"29","statusdesc":"love29.jpg"},{"statusid":"28","statusdesc":"love28.jpg"},{"statusid":"27","statusdesc":"love27.jpg"},{"statusid":"26","statusdesc":"love26.jpg"},{"statusid":"25","statusdesc":"love25.jpg"},{"statusid":"24","statusdesc":"love24.jpg"},{"statusid":"23","statusdesc":"love23.jpg"},{"statusid":"22","statusdesc":"love22.jpg"},{"statusid":"21","statusdesc":"love21.jpg"},{"statusid":"20","statusdesc":"love20.jpg"},{"statusid":"19","statusdesc":"love19.jpg"},{"statusid":"18","statusdesc":"love18.jpg"},{"statusid":"17","statusdesc":"love17.jpg"},{"statusid":"16","statusdesc":"love16.jpg"},{"statusid":"15","statusdesc":"love15.jpg"},{"statusid":"14","statusdesc":"love14.jpg"},{"statusid":"13","statusdesc":"love13.jpg"},{"statusid":"12","statusdesc":"love12.jpg"},{"statusid":"11","statusdesc":"love11.jpg"},{"statusid":"10","statusdesc":"love10.jpg"},{"statusid":"9","statusdesc":"love9.jpg"},{"statusid":"8","statusdesc":"love8.jpg"},{"statusid":"7","statusdesc":"love7.jpg"},{"statusid":"6","statusdesc":"love6.jpg"},{"statusid":"5","statusdesc":"love5.jpg"},{"statusid":"4","statusdesc":"love4.jpg"},{"statusid":"3","statusdesc":"love3.jpg"},{"statusid":"2","statusdesc":"love2.jpg"},{"statusid":"1","statusdesc":"love1.jpg"}]}';

    $response_array = json_decode($response,true);

    $final_res =  array_column($response_array['posts'],'statusdesc');

    print_r($final_res);

?>

输出:

Array
(
    [0] => love86.jpg
    [1] => love85.jpg
    [2] => love84.jpg
    [3] => love83.jpg
    [4] => love82.jpg
    [5] => love81.jpg
    [6] => love80.jpg
    ......
 )

答案 3 :(得分:0)

$json_url = "http://digitsolutions4u.com/apps_services/statuslist.php?catid=1";
$json = file_get_contents($json_url);

这应该返回你的对象数组(stdClass)

$elm = json_decode( $json );
var_dump( $elm->posts );

你可以用这种方式得到你的一个字符串:

$elm->posts[5]->statusdesc

或者你可以使用json_decode($ json,true)将你的json转换为数组

注意:json_decode不能记住您的原始类为serialize它总是返回stdClass(您可以访问属性但不能访问原始类的方法)