如何在 RESTFUL API 中从 JSON 文件中获取随机 10 条记录



以下是我的代码,请检查并告诉如何从json中获取10条随机记录并希望显示在选框标签中。提前感谢您>请检查我获得 10 条记录的代码,但想>随机显示。谢谢

<html>
    <head>
        <meta charset="UTF-8">
            <title>Sabkideal.com Todays findmystay.com</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script> <!-- Boostarp -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" media="all" /> 
<!-- Boostrap -->
<link rel="stylesheet" href="bootstrap/css/custom.css" media="all" />
<script type="text/javascript">

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
    target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    target.style.MozUserSelect="none"
else //All other route (ie: Opera)
    target.onmousedown=function(){return false}
target.style.cursor = "default"
}
//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"
</script>
<style>
.button {
  background-color: #FF5733;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  border: none;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
  font-family: Arial;
  font-size: 20px;
  padding: 5px 10px;
  text-align: center;
  text-decoration: none;
}
@-webkit-keyframes glowing {
  0% { background-color: #B20000; -webkit-box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; -webkit-box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; -webkit-box-shadow: 0 0 3px #B20000; }
}
@-moz-keyframes glowing {
  0% { background-color: #B20000; -moz-box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; -moz-box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; -moz-box-shadow: 0 0 3px #B20000; }
}
@-o-keyframes glowing {
  0% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
}
@keyframes glowing {
  0% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
}
.button {
  -webkit-animation: glowing 1500ms infinite;
  -moz-animation: glowing 1500ms infinite;
  -o-animation: glowing 1500ms infinite;
  animation: glowing 1500ms infinite;
}
</style>
    </head>
    <body>
          <nav class="navbar navbar-default" role="navigation">
     <div class="container-fluid">
<!--header section -->
          <div class="navbar-header">
               <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
               <span class="sr-only">Toggle navigation</span>
               <span class="icon-bar"></span>
               <span class="icon-bar"></span>
               <span class="icon-bar"></span>
               </button>
               <a class="navbar-brand" href="index.php">Sabkideal.com  </a>
          </div>
<!-- menu section -->
          <div class="collapse navbar-collapse navbar-ex1-collapse">
               <ul class="nav navbar-nav navbar-right">
               <li><a href="# ">Home</a></li>
               <li><a href="# ">Register and win </a></li>
               <li><a href="#">Conatct Us </a></li>
               </ul>
          </div>
     </div>
</nav>
<div style="margin-left: 150px;
     width: 100px; ">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sabkidealLeaderborad -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-9170404078755708"
     data-ad-slot="9181804474"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div class ="container">
<div class ="row">
<div class="col-lg-2">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sabkidealskycraper -->
<ins class="adsbygoogle"
     style="display:inline-block;width:160px;height:600px"
     data-ad-client="ca-pub-9170404078755708"
     data-ad-slot="3308237676"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div class="col-lg-8">
        <?php
        $c = 0; 
    $ch = curl_init("http://tools.vcommission.com/api/coupons.php?apikey=e159f64e3dd49fddc3bb21dcda70f10c6670ea91aac30c7cb1d4ed37b20c45b8"); // add your url which contains json file
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($content, true);
    //print_R($json);
    $count=count($json);

    for($i=0;$i<$count;$i++)
    {

        echo '<marquee behavior="scroll" direction="left" onmouseover="this.stop();" onmouseout="this.start();">';
      echo'
      <a target="_blank" style="width:100%" rel="nofollow, noindex" href='.$json[$i]['link'].'>  
<p>'.$json[$i]['coupon_title'].'</p></a>';


       echo '</marquee>';
        }



?>
</div>
<script type="text/javascript">
var alltables=document.getElementsByTagName("table")
for (var i=0; i<alltables.length; i++)
disableSelection(alltables[i]) //disable text selection within all tables on the page
</script>
<div class="col-lg-2">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sabkidealskycraper -->
<ins class="adsbygoogle"
     style="display:inline-block;width:160px;height:600px"
     data-ad-client="ca-pub-9170404078755708"
     data-ad-slot="3308237676"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</div>
</div>

    </body>
</html>

您正在寻找 php 函数array_rand

.PHP

$json = json_decode($content, true);
$randoms = array_rand($json, 10);
$count = count($randoms);

for($i=0;$i<$count;$i++)
{
      echo '<marquee behavior="scroll" direction="left" onmouseover="this.stop();" onmouseout="this.start();">';
      echo'
      <a target="_blank" style="width:100%" rel="nofollow, noindex" href='.$json[$randoms[$i]]['link'].'>
      <p>'.$json[array_rand($json)]['coupon_title'].'</p></a>';
      echo '</marquee>';
}