Привет У меня есть результат массива, который генерируется следующим кодом:
//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);
Результаты запроса можно посмотреть здесь.
То, что мне нужно, это сделать для каждого результата, вставить значения в другую таблицу, пока я смотрю на что-то вроде этого:
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;
Но результаты возвращаются как null, что я делаю неправильно, пожалуйста>
Вы пробовали это:
foreach ($json2->result->sales as $item) {
$pricepaid = $item->pricePaidGBP;
$propertytype = $item->propertyType;
$holding = $item->holding;
$nameornumber = $item->nameOrNumber;
$fulllocation = $item->fullLocation;
$dateoftransfer = $item->dateOfTransfer;
...
}
Преобразовать в массив -
$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
$pricepade = $item1->pricePaidGBP
. иначе зачем заморачиваться на "-> result-> sales", а затем полностью игнорировать переменную, которую цикл создает для вас?