Как мне получить доступ к этому массиву

0

Привет У меня есть результат массива, который генерируется следующим кодом:

//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, что я делаю неправильно, пожалуйста>

  • 1
    вероятно, хотите $pricepade = $item1->pricePaidGBP . иначе зачем заморачиваться на "-> result-> sales", а затем полностью игнорировать переменную, которую цикл создает для вас?
Теги:

2 ответа

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;
    ...
}
  • 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
  • 0
    Это объект prntscr.com/59okbm .
  • 0
    это выглядит как массив внутри объекта
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню