1

does not work with a nested element the elements of the first level are output, and the nested array with data is not read, as it is possible to get values - id, title and location?

<?php

function removeBomUtf8($s){
  if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
        return substr($s,3);
    }else{
        return $s;
    }
}

$url = "https://denden000qwerty.000webhostapp.com/opportunities.json";
$content = file_get_contents($url);
$clean_content = removeBomUtf8($content);
$decoded = json_decode($clean_content);

while ($el_name = current($decoded)) {
 // echo 'total = ' . $el_name->total_items . 'current = ' . $el_name->current_page . 'total = ' . $el_name->total_pages . '<br>' ;
    echo ' id = ' . $el_name->data[0]->id . ' title = ' . $el_name->data.title . ' location = ' . $el_name->data.location . '<br>' ;
  next($decoded);
}
?>
3
  • what are the things you want to parse? Commented Sep 22, 2017 at 6:38
  • "data":[{"id":412235,"title":"We Speak English","lat":"10.6966159","lng":"-74.8741045","url":"gis.aiesec.org/v2/opportunities/…, Colombia","city":"Atlántico, Colombia","programmes":{"id":1,"short_name":"GV"},"applications_count":54,"is_favourited":false,"branch":{"id":321136,"name":"@ UNIATLÃNTICO","organisation": etc... Commented Sep 22, 2017 at 6:45
  • i need get - "id":412235,"title":"We Speak English" city":"Atlántico, Colombia" Commented Sep 22, 2017 at 6:45

1 Answer 1

2

$el_name->data[0]->id is correct

$el_name->data.title is not

you see the difference?

and $decoded is the root (no need to iterate over it) - you want to iterate over the data children

<?php
foreach($decoded->data as $data)
{
    $id = (string)$data->id;
    $title = (string)$data->title;
    $location = (string)$data->location;


    echo sprintf('id = %s, title = %s, location = %s<br />', $id, $title, $location);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.