Your foreach
just pulls the array_keys()
so can be dropped. You can also use array_splice()
to get the first 5 records as noted in the comments by Pelippe Duarte.
$response = json_decode(file_get_contents('vid_id.json'), true);
asort($response);
$top5 = array_splice( // Split
array_values( // Loose Original Keys
array_reverse( // I Assume For Sorting
array_keys($response['video_id']) // Dropped Foreach
)
)
, 0, 4); // Offset 0, Length
This is untested as I do not know what your vid_id.json
holds but theoretically, this should work.
$top5
will now be an array you can loop through on your front-end and output rather than your back-end.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…