如何在PHP for Wordpress Plugin中迭代JSON数据

时间:2016-01-16 23:34:00

标签: php json wordpress

我正在构建一个调用第三方API的插件。我正在获取数据,但它看起来有点时髦,我无法迭代和操纵数据显示在我的页面上。

我正在使用此函数调用正在运行的API ...

function herocreative_rentcafe_get_data( $herocreative_companycode, $herocreative_propertycode ) {

    $json_feed_url = 'https://api.rentcafe.com/rentcafeapi.aspx?requestType=floorplan&companyCode=' . $herocreative_companycode . '&propertyCode=' . $herocreative_propertycode ;
    $args = array( 'timeout' => 120 );

    $json_feed = wp_remote_get( $json_feed_url, $args );

    $herocreative_data = json_decode( $json_feed['body'] );

    return $herocreative_data;

} 

我正在使用此代码来迭代响应......

   <?php for( $i = 0; $i < count($herocreative_data); $i++ ): ?>
    <li>
        <ul>
            <li>
                <?php echo $herocreative_data->{'AvailableUnitsCount'}[$i]; ?>              
            </li>
        </ul>                                   
    </li>                               
    <?php endfor; ?>

回来的数据是:

JSON Feed

    array(3) {
  [0]=>
  object(stdClass)#321 (18) {
    ["PropertyId"]=>
    string(6) "457547"
    ["FloorplanId"]=>
    string(7) "2027167"
    ["FloorplanName"]=>
    string(11) "One Bedroom"
    ["Beds"]=>
    string(1) "1"
    ["Baths"]=>
    string(4) "1.00"
    ["MinimumSQFT"]=>
    string(3) "714"
    ["MaximumSQFT"]=>
    string(3) "714"
    ["MinimumRent"]=>
    string(4) "1125"
    ["MaximumRent"]=>
    string(4) "1355"
    ["MinimumDeposit"]=>
    string(1) "1"
    ["MaximumDeposit"]=>
    string(1) "1"
    ["AvailableUnitsCount"]=>
    string(1) "5"
    ["AvailabilityURL"]=>
    string(137) "https://epicasset.securecafe.com/onlineleasing/park-210/oleapplication.aspx?stepname=Apartments&myOlePropertyId=457547&floorPlans=2027167"
    ["FloorplanImageURL"]=>
    string(65) "http://cdn.rentcafe.com/dmslivecafe/3/457547/3_457547_2310392.jpg"
    ["FloorplanImageName"]=>
    string(7) "1x1.jpg"
    ["PropertyShowsSpecials"]=>
    string(1) "0"
    ["FloorplanHasSpecials"]=>
    string(1) "0"
    ["UnitTypeMapping"]=>
    string(6) "p0.1x1"
  }
  [1]=>
  object(stdClass)#323 (18) {
    ["PropertyId"]=>
    string(6) "457547"
    ["FloorplanId"]=>
    string(7) "2027168"
    ["FloorplanName"]=>
    string(20) "Two Bedroom One Bath"
    ["Beds"]=>
    string(1) "2"
    ["Baths"]=>
    string(4) "1.00"
    ["MinimumSQFT"]=>
    string(4) "1000"
    ["MaximumSQFT"]=>
    string(4) "1000"
    ["MinimumRent"]=>
    string(4) "1325"
    ["MaximumRent"]=>
    string(4) "1455"
    ["MinimumDeposit"]=>
    string(1) "0"
    ["MaximumDeposit"]=>
    string(1) "0"
    ["AvailableUnitsCount"]=>
    string(1) "0"
    ["AvailabilityURL"]=>
    string(137) "https://epicasset.securecafe.com/onlineleasing/park-210/oleapplication.aspx?stepname=Apartments&myOlePropertyId=457547&floorPlans=2027168"
    ["FloorplanImageURL"]=>
    string(65) "http://cdn.rentcafe.com/dmslivecafe/3/457547/3_457547_2310393.jpg"
    ["FloorplanImageName"]=>
    string(7) "2x1.jpg"
    ["PropertyShowsSpecials"]=>
    string(1) "0"
    ["FloorplanHasSpecials"]=>
    string(1) "0"
    ["UnitTypeMapping"]=>
    string(6) "p0.2x1"
  }
  [2]=>
  object(stdClass)#324 (18) {
    ["PropertyId"]=>
    string(6) "457547"
    ["FloorplanId"]=>
    string(7) "2027169"
    ["FloorplanName"]=>
    string(20) "Two Bedroom Two Bath"
    ["Beds"]=>
    string(1) "2"
    ["Baths"]=>
    string(4) "2.00"
    ["MinimumSQFT"]=>
    string(4) "1095"
    ["MaximumSQFT"]=>
    string(4) "1095"
    ["MinimumRent"]=>
    string(4) "1395"
    ["MaximumRent"]=>
    string(4) "1525"
    ["MinimumDeposit"]=>
    string(1) "0"
    ["MaximumDeposit"]=>
    string(1) "0"
    ["AvailableUnitsCount"]=>
    string(1) "0"
    ["AvailabilityURL"]=>
    string(137) "https://epicasset.securecafe.com/onlineleasing/park-210/oleapplication.aspx?stepname=Apartments&myOlePropertyId=457547&floorPlans=2027169"
    ["FloorplanImageURL"]=>
    string(65) "http://cdn.rentcafe.com/dmslivecafe/3/457547/3_457547_2310394.jpg"
    ["FloorplanImageName"]=>
    string(7) "2x2.jpg"
    ["PropertyShowsSpecials"]=>
    string(1) "0"
    ["FloorplanHasSpecials"]=>
    string(1) "0"
    ["UnitTypeMapping"]=>
    string(6) "p0.2x2"
  }
}

1 个答案:

答案 0 :(得分:1)

您需要做的就是在数组上循环,然后每个出现包含一个对象地址就这样

config