Php 如何循环RESTAPI响应?
我通过REST API检索帖子,如下所示:Php 如何循环RESTAPI响应?,php,wordpress,Php,Wordpress,我通过REST API检索帖子,如下所示: $response = wp_remote_get(get_home_url()."2/wp-json/wp/v2/posts?include=".$postId); $data = json_decode($response['body'], true); 那么我有: 'acf' => array ( 'descrizione_lavoro' => ' Lorem ipsum dolor ',
$response = wp_remote_get(get_home_url()."2/wp-json/wp/v2/posts?include=".$postId);
$data = json_decode($response['body'], true);
那么我有:
'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',
我需要获取url
,所以我需要:
var_dump($data[0]['acf']["galleria"][0]['url']);
工作正常,但我在galleria下有不止一个索引,因此如果我这样做:
var_dump($data[0]['acf']["galleria"][1]['url']);
我将获得第二张图像,但可能有很多,我如何循环以检索所有图像???使用
foreach( $data[0]['acf']["galleria"] as $gallery ) {
echo $gallery['url'];
}
尝试
foreach($data[0]['acf'][“galleria”]as$gallery){echo$gallery['url'];}
@TamilSelvanC perfect,这是我的想法,但我不确定是否要循环该路径。非常感谢,把它写进一个答案,我会接受的。我的问题不是从json中提取数据,也不是重复的