dongqiao1158 2016-02-28 14:23
浏览 51
已采纳

给定特定值时显示数组键

I have an array of values queried from a database, $roundsarray.

Another query returns a set of values which are some of the same in the array. However, rather than displaying the values, I want to display them as a key. My code is as follows:

$datarounds=mysql_query("SELECT DISTINCT eventID FROM results WHERE (eventID BETWEEN '$firstevent' AND '$lastevent') AND compId='$compId' AND teamId='$teamid' AND (eventSession='R' OR eventSession='R1' OR eventSession='R2' OR eventSession='R3') AND driverId='$driversource' ORDER BY eventID", $CONNECTW);        
while($row=mysql_fetch_row($datarounds))
{ 
    $roundid=$row[0];
    foreach ($roundsarray as $k => $value)
    {
        if ($roundid==$value) { $rounddisplay=$k+1; }

        $roundpack .= "$rounddisplay,";
    }

The $datarounds query provides the event ids, and what i am trying to do is to display that a specific event is the 3rd, 4th, 12th or whatever in the array (the $k+1 is to account for the first value of the array being key 0).

However, $rounddisplay is always returned empty and I can't figure out what I am doing wrong. Thank you for any help!

  • 写回答

1条回答 默认 最新

  • dongxian8048 2016-02-28 18:04
    关注

    You should look for: array_search to get the keys.

    Check out this link php.net/manual/en/function.array-search.php use array_search to search for the keys and then map it into new array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器