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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?