foreach 只显示一个 JSON 值
foreach only displaying one JSON value
我一直在努力 foreach
。对于数组中的最后一个元素,它只给我一个结果。
if (isset($_POST['Title'])){
$artist = $_POST['Title'];
$artist = str_replace(' ', '%20', $artist);
$Json = file_get_contents('http://developer.echonest.com/api/v4/song/search?api_key=HT72O1OINP8ERYDE9&artist='.$artist.'&bucket=audio_summary');
$decode = json_decode($Json);
$songList = array();
foreach ($decode -> response -> songs as $song)
{
$songList [] = array(
$SongName = $song->title,
$songId = $song->id,
$artist = $song->artist_name,
$bpm = $song->audio_summary->tempo
);
};
}
?>
这是我的 html 代码:
<?php $i = 1;
foreach ($decode -> response -> songs as $test) {
echo "<tr>
<th>".$i."</th>
<th>".$SongName."</th>
<th>".$artist."</th>
<th>".$time."</th>
<th>".$bpm."</th></tr>";
++ $i;
}
?>
给出的结果不断重复,而不是遍历整个数组。
Oook,所以你是 运行 同一数据的两个循环。在第一个循环中,您填充一些变量,然后退出该循环。
在第二个循环中,您再次遍历所有行,但不是从那里选择值,而是使用您在上一个循环中填充的变量,当然,这些变量在每次迭代中都会被覆盖,您只能看到数据最后一行。
你的第一个循环没用,你只需要一个循环,看起来像这样
$i = 1;
foreach ($decode -> response -> songs as $test) {echo
"<tr>
<th>".$i."</th>
<th>".$song->title."</th>
<th>".$song->artist_name."</th>
<th>".$time."</th>
<th>".$song->audio_summary->tempo."</th></tr>";
++ $i;
}
我一直在努力 foreach
。对于数组中的最后一个元素,它只给我一个结果。
if (isset($_POST['Title'])){
$artist = $_POST['Title'];
$artist = str_replace(' ', '%20', $artist);
$Json = file_get_contents('http://developer.echonest.com/api/v4/song/search?api_key=HT72O1OINP8ERYDE9&artist='.$artist.'&bucket=audio_summary');
$decode = json_decode($Json);
$songList = array();
foreach ($decode -> response -> songs as $song)
{
$songList [] = array(
$SongName = $song->title,
$songId = $song->id,
$artist = $song->artist_name,
$bpm = $song->audio_summary->tempo
);
};
}
?>
这是我的 html 代码:
<?php $i = 1;
foreach ($decode -> response -> songs as $test) {
echo "<tr>
<th>".$i."</th>
<th>".$SongName."</th>
<th>".$artist."</th>
<th>".$time."</th>
<th>".$bpm."</th></tr>";
++ $i;
}
?>
给出的结果不断重复,而不是遍历整个数组。
Oook,所以你是 运行 同一数据的两个循环。在第一个循环中,您填充一些变量,然后退出该循环。
在第二个循环中,您再次遍历所有行,但不是从那里选择值,而是使用您在上一个循环中填充的变量,当然,这些变量在每次迭代中都会被覆盖,您只能看到数据最后一行。
你的第一个循环没用,你只需要一个循环,看起来像这样
$i = 1;
foreach ($decode -> response -> songs as $test) {echo
"<tr>
<th>".$i."</th>
<th>".$song->title."</th>
<th>".$song->artist_name."</th>
<th>".$time."</th>
<th>".$song->audio_summary->tempo."</th></tr>";
++ $i;
}