1

I retrieve a post via REST API as per the following:

$response = wp_remote_get(get_home_url()."2/wp-json/wp/v2/posts?include=".$postId);
$data = json_decode($response['body'], true);

Then I have:

'acf' => 
  array (
    'descrizione_lavoro' => '
          Lorem ipsum dolor 
    ',
    'location' => 
    array (
      'lat' => '41.36256133817761',
      'lng' => '2.131976960327165',
    ),
    'categories' => 
    array (
      0 => 627,
    ),
    'partner' => 
    array (
      0 => 'Sandra Yannis',
    ),
    'collaboratori' => 
    array (
      0 => 'Fran White',
      1 => 'Sam Jumper',
    ),
    'importo' => '1200000',
    'galleria' => 
    array (
      0 => 
      array (
        'ID' => 128345,
        'id' => 128345,
        'title' => 'villa1',
        'filename' => 'villa1.jpg',
        'filesize' => 78350,
        'url' => 'http://example.com/ing2/wp-content/uploads/2019/03/villa1.jpg',

And I need to get url so I do:

var_dump($data[0]['acf']["galleria"][0]['url']);

And works fine, but I have more than one index under galleria therefore if I do:

var_dump($data[0]['acf']["galleria"][1]['url']);

I will get the second image but there could be plenty, how would I loop in order to retrieve all images??

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 1
    try `foreach( $data[0]['acf']["galleria"] as $gallery ) { echo $gallery['url']; }` – Tamil Selvan C Jul 31 '19 at 02:19
  • @TamilSelvanC perfect, that's what I thought but I wasn't sure about looping that path. Thanks a lot, put that into an answer and I will accept it. – rob.m Jul 31 '19 at 02:20
  • my question isn't exactly about extracting data from json and it's not a duplicate – rob.m Jul 31 '19 at 02:45

1 Answers1

2

Use

foreach( $data[0]['acf']["galleria"] as $gallery ) { 
    echo $gallery['url']; 
} 
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70