无法从mysql多维数组中获取键值

时间:2016-10-05 10:29:31

标签: php mysql arrays

我正在尝试从我使用的多维数据库数组中获取键值。在代码之后给出了数组快照。

以下是我的PHP代码 -

 $selectTicket =  "select ticketID from ticketusermapping where userID=$userID and distanceofticket <=$miles;";
$rsTicket = mysqli_query($link,$selectTicket);
$numOfTicket = mysqli_num_rows($rsTicket);  
if($numOfTicket > 0){
    $allRowData = array();  
while($row = mysqli_fetch_assoc($rsTicket)){
    $allRowData[] = $row;
}
 $key = 'array(1)[ticketID]';
    $QueryStr = "SELECT * FROM ticket WHERE ticketID IN (".implode(',', array_keys($key)).")";

阵列快照 - enter image description here

我需要来自此数组的tickedID值。就像第一个是49。 请帮忙。

3 个答案:

答案 0 :(得分:1)

$ids = array_column( $allRowData, 'ticketID'); //this will take all ids as new array
 $QueryStr = "SELECT * FROM ticket WHERE ticketID IN (".implode(',', $ids).")";

答案 1 :(得分:1)

更改您的代码,如

$selectTicket = "select ticketID from ticketusermapping where userID=$userID and distanceofticket <=$miles;";
    $rsTicket = mysqli_query($link, $selectTicket);
    $numOfTicket = mysqli_num_rows($rsTicket);
    if ($numOfTicket > 0) {
        $allRowData = array();
        while ($row = mysqli_fetch_assoc($rsTicket)) {
            $allRowData[] = $row['ticketID'];
        }
        $QueryStr = "SELECT * FROM ticket WHERE ticketID IN (" . implode(',', $allRowData) . ")";

答案 2 :(得分:0)

您应该使用JOIN执行单个查询:

$query = "
  SELECT t.* 
  FROM ticket t 
  JOIN ticketusermapping tum 
    ON t.ticketID = tum.ticketID
   AND tum.userID = '$userID'
   AND tum.distanceofticket <= '$miles'
";

$stmt = mysqli_query($link, $query);

$numOfTickets = mysqli_num_rows($stmt);

while($row = mysqli_fetch_assoc($stmt)){
    var_dump($row); // here will be the ticket data
}