【问题标题】:How do I insert values into an multidimensional-array, then show them?如何将值插入多维数组,然后显示它们?
【发布时间】:2013-12-08 19:34:00
【问题描述】:

我是 php 的新手,我不知道如何很好地使用数组。这是交易,我想将我从数据库中获得的三个或更多值添加到多维数组中,然后我想根据时间戳(其中一个值)对它们进行排序。之后,我想显示所有排序的值。我似乎无法做到这一点,这是代码

    $queryWaitingPatients = 'SELECT ArrivalTime, TargetTime, Order, Classification FROM exams WHERE (CurrentState = "Pending")';

    $results = mysql_query($queryWaitingPatients) or die(mysql_error());

    if (mysql_num_rows($results) == 0) {
        echo '<p>There\'s currently no patient on the waiting list.</p>';
        return;
    }
    while ($rows = mysql_fetch_array($results)) {
        extract($rows);
    //now is the part that I don't know, putting the values into an array
    }
    // I'm also not sure how to sort this according to my $TargetTime
    asort($sortedTimes);

     //the other part I don't know, showing the values, 

感谢您的帮助!

【问题讨论】:

  • 仅供参考: orderreserved word。用反引号括起来或使用其他词,例如Orders
  • 为什么不在 sql 查询中使用 ORDER BY?这样数据就已经排序了。

标签: php sql arrays sorting multidimensional-array


【解决方案1】:

好吧,让我们看看你的代码。首先,您有一个返回结果集的查询。我不建议使用mysql_fetch_array,因为它不仅是deprecated(改用mysqli 函数),而且它往往会导致糟糕的代码。当所有的键都是数字时,很难弄清楚你在引用什么。所以我推荐mysqli_fetch_assoc(确保你首先完全切换到mysqli函数,比如mysql_connectmysqli_query

其次,我真的不喜欢使用提取。我们需要直接使用数组。以下是我们的做法

$myarray = array();
while ($rows = mysqlI_fetch_assoc($results)) {
    $myarray[] = $rows;
}
echo $myarray[0]['ArrivalTime'];

所以让我们回顾一下。首先,我们正在构建一个数组数组。所以我们初始化我们的整体array。然后我们要将行推送到这个数组上。这就是$myarray[] 所做的。最后,我们推送的数组是关联的,这意味着该行的所有键都与您查询的字段名称匹配。

现在,确实需要在查询中进行排序。因此,让我们调整您的查询

$queryWaitingPatients = 'SELECT ArrivalTime, TargetTime, `Order`, Classification 
    FROM exams 
    WHERE CurrentState = "Pending"
    ORDER BY TargetTime';

这样,当您的 PHP 运行时,您的数据库现在会以正确的顺序为您的数组生成它们。无需排序代码。

【讨论】:

  • 非常感谢!这真的很有见地和详细。我还是一个新手,因为我不知道很多功能,这让我有点受限制。至于排序,我稍后会需要它,在更改 $myarray 之后
【解决方案2】:
$arr = array(); 
while ($rows = mysql_fetch_array($results)) {
     array_push ($arr, $row);
}

print_r($arr);

【讨论】:

    【解决方案3】:
    <?php
    $queryWaitingPatients = ' SELECT ArrivalTime, TargetTime, Order, Classification, CurrentState 
                              FROM exams 
                              WHERE CurrentState = "Pending"
                              ORDER BY TargetTime ';
    
    $results = mysql_query($queryWaitingPatients) or die(mysql_error());
        if ($results -> num_rows < 1)
        {
            echo '<p>There\'s currently no patient on the waiting list.</p>';
         }
         else
        {
         while ($rows = mysqli_fetch_array($results))
           {
             $arrivaltime = $row['ArrivalTime'];
             $targettime = $row['targettime'];
             $order = $row['Order'];
             $classification = $row['Classification'];
             echo   "Arrival: ".$arrivaltime."--Target time: ".$targettime."--Order: ".$order."--Classification: ".$classification;
            }
         }
    echo "Done!";
    //or you could put it in a json array and pass it to client side.
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2019-11-02
      • 2019-04-01
      • 1970-01-01
      • 2021-11-22
      • 2011-12-16
      相关资源
      最近更新 更多