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 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计