我已经通过api检索了多维数组,但是我不愿意从数组中检索值
请检查下面的代码
<div class="container">
<h2>Our Partners/ Our Clients</h2>
<section class="customer-logos slider">
<?php for ($i=0; $i <count($response) ; $i++)
{
?>
<div class="slide">
<img src="<?php echo $response['urlToImage'][$i]; ?>">
</div>
<?php }?>
</section>
</div>
使用上述代码,我无法正确获取数据
下面是我需要值的数组
Array
(
[title] => Array
(
[0] => Saudi energy minister: We'd work with anyone interested in
balancing the oil market - CNBC
[1] => Gurley, Rams run through Cowboys, advance with 30-22 victory
- Fox News
[2] => NFL Conference Semifinals (Call It That!): Foles Hasn't Been
Tested Like This, Chargers’ Fatal Flaw - Sports Illustrated
)
[description] => Array
(
[0] => Saudi Energy Minister Khalid al-Falih told CNBC on Sunday that his country "will work with all interested producers who want to bring stability to the market ... OPEC plus and anybody else who would like to do it with us.
[1] => The high-flying Los Angeles Rams kept the ball firmly on the ground, and they ran straight past the Dallas Cowboys for a breakthrough playoff victory.
[2] => Also, the desperate scramble to win the press conference, what the eight conference semifinal coaches were when they were hired, and the latest coaching fashion trend. Plus, an encore performance from The Mountain Goats!
)
[url] => Array
(
[0] => https://www.cnbc.com/2019/01/13/saudi-energy-minister-on-work-with-oil-producers-to-balance-market.html
[1] => https://www.foxnews.com/sports/gurley-rams-run-through-cowboys-advance-with-30-22-victory
[2] => https://www.si.com/nfl/2019/01/13/playoffs-patriots-chargers-eagles-saints-conference-semifinals-divisional-round-coach-hirings
)
[urlToImage] => Array
(
[0] => https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2018/06/22/105288796-GettyImages-981021686.1910x1000.jpg
[1] => https://static.foxnews.com/foxnews.com/content/uploads/2019/01/rams-cowboys-gurley.jpg
[2] => https://imagesvc.timeincapp.com/v3/mm/image?url=https://cdn-s3.si.com/s3fs-public/2019/01/12/football-things-conference-semifinals.jpg&w=1200&h=628&c=sc&poi=face&q=85
)
答案 0 :(得分:0)
因此,假设您仅尝试遍历图像URL,请考虑以下代码:
<div class="container">
<h2>Our Partners/ Our Clients</h2>
<section class="customer-logos slider">
<?php if (isset($response['urlToImage'])) : ?>
<?php foreach ($response['urlToImage'] as $imageUrl): ?>
<div class="slide">
<img src="<?= $imageUrl; ?>">
</div>
<?php endforeach; ?>
<?php endif; ?>
</section>
</div>