我如何访问此阵列

时间:2014-11-24 16:51:30

标签: php

您好我有一个数组结果,它是由以下代码生成的:

//Get House Sales Date
// Create a stream
$username = "test";
$password = "test";
$remote_url2 = "https://www.streetcheck.co.uk/api/v1/postcode/".$cleanpostcode."/saleprices";
$opts2 = array(  'http'=>array(    'method'=>"GET", 'header' => "Authorization: Basic " . base64_encode("$username:$password")));
$context2 = stream_context_create($opts2);

//Open the file using the HTTP headers set above
$json2 = json_decode(file_get_contents($remote_url2, false, $context2));

//If Valid Results Returned
if($json2->message="ok")
{
    print_r($json2);

可以查看查询结果here

我需要做的是为每个结果,将值插入另一个表,到目前为止,我正在看这样的事情:

foreach ($json2->result->sales as $item1)
{
    $pricepaid = $json2->result->sales->pricePaidGBP;
    $propertytype = $json2->result->sales->propertyType;
    $holding = $json2->result->sales->holding;
    $nameornumber = $json2->result->sales->nameOrNumber;
    $fulllocation = $json2->result->sales->fullLocation;
    $dateoftransfer = $json2->result->sales->dateOfTransfer;

但结果又回来了,我做错了,请>

2 个答案:

答案 0 :(得分:1)

转换为数组 -

$json2 = json_decode(file_get_contents($remote_url2, false, $context2), true);

然后你可以为每个项目检索这个 -

foreach ($json2 as $item)
  {
  $pricepaid = $item['result']['sales']['pricePaidGBP'];
  // ... the rest of the items

答案 1 :(得分:1)

你试过这个:

foreach ($json2->result->sales as $item) {
    $pricepaid = $item->pricePaidGBP;
    $propertytype = $item->propertyType;
    $holding = $item->holding;
    $nameornumber = $item->nameOrNumber;
    $fulllocation = $item->fullLocation;
    $dateoftransfer = $item->dateOfTransfer;
    ...
}