0
$pf = new Petfinder('API_KEY');
$pf->setResponseFormat('json');
$petJson = $pf->pet_find(array('output'=>'basic', 'location' => '32810', 'count' => '25'));
$pet = json_decode($petJson);
foreach ($pet as $pets)
{
    print_r($pets);
}

我试过var_dump($pets->pets->pet);了,但没有用。

/* 回复 */

iso-8859-11.0stdClass Object
(
    [lastOffset] => stdClass Object
    (
        [$t] => 1
    )
    [pets] => stdClass Object
    (
        [pet] => stdClass Object
        (
            [options] => stdClass Object
            (
                [option] => stdClass Object
                (
                    [$t] => housebroken
                )
            )
            [breeds] => stdClass Object
            (
                [breed] => stdClass Object
                (
                    [$t] => Domestic Short Hair
                )
            )
            [shelterPetId] => stdClass Object()
            [status] => stdClass Object
            (
                [$t] => A
            )
            [name] => stdClass Object
            (
                [$t] => Bottle Fed Kittens
            )
            [contact] => stdClass Object
            (
                [email] => stdClass Object
                (
                    [$t] => catboutcolonies@yahoo.com
                )    
                [zip] => stdClass Object
                (
                    [$t] => 32810
                )    
                [city] => stdClass Object
                (
                    [$t] => Orlando
                )
                [fax] => stdClass Object()
                [name] => stdClass Object
                (
                    [$t] => Lori Lewis
                )
                [address1] => stdClass Object
                (
                    [$t] => P.O.Box 607082
                )
                [phone] => stdClass Object
                (
                    [$t] => 407-953-0297
                )
            )
            [description] => stdClass Object
            (
                [$t] =>
            )
            [sex] => stdClass Object
            (
                [$t] => F
            )
            [media] => stdClass Object
            (
                [photos] => stdClass Object
                (
                    [photo] => Array
                    (
                        [0] => stdClass Object
                        (
                            [@size] => x
                            [$t] => http://photos.petfinder.com/photos/US/FL/FL1155/26769037/FL1155.26769037-1-x.jpg
                            [@id] => 1
                        )
                        [2] => stdClass Object
                        (
                            [@size] => t
                            [$t] => http://photos.petfinder.com/photos/US/FL/FL1155/26769037/FL1155.26769037-3-t.jpg
                            [@id] => 3
                        )
                    )
                )
            )
            [id] => stdClass Object
            (
                [$t] => 26769037
            )
            [animal] => stdClass Object
            (
                [$t] => Cat
            )
        )
    )
    [header] => stdClass Object
    (
        [timestamp] => stdClass Object
        (
            [$t] => 2013-08-13T05:54:46Z
        )
        [status] => stdClass Object
        (
            [message] => stdClass Object()
            [code] => stdClass Object
            (
                [$t] => 100
            )
        )
        [version] => stdClass Object
        (
            [$t] => 0.1
        )
    )
    [@xsi: noNamespaceSchemaLocation] = > http: //api.petfinder.com/schemas/0.9/petfinder.xsd
)

我只是想提取一个元素,例如品种,任何帮助将不胜感激。

4

3 回答 3

1

要获得 Breed,您需要执行以下操作。

$pf = new Petfinder('API_KEY');
$pf->setResponseFormat('json');
$petJson = $pf->pet_find(array('output'=>'basic', 'location' => '32810', 'count' => '25'));
$pet = json_decode($petJson);

变量转储

var_dump($pet->pets->pet->breeds->breed);
于 2013-08-13T06:52:20.360 回答
0

您可以将此对象转换为数组并使用数组函数,请尝试使用此代码进行转换。

if (is_object($pets)) {
    $pets_arr = get_object_vars($pets);
    var_dump($pets_arr);
}
于 2013-08-13T06:41:44.803 回答
0

这是我如何解析品种的方法。

    var_dump($pet->petfinder->pets->pet->breeds->breed)  

回复

   array(2) { [0]=> object(stdClass)#25 (1) { ["$t"]=> string(19) "Domestic Short Hair" }          [1]=> object(stdClass)#26 (1) { ["$t"]=> string(36) "Domestic Short Hair -Gray And White" } }
于 2013-08-13T18:51:50.833 回答