首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php 2D数组的问题

问php 2D数组的问题
EN

Stack Overflow用户
提问于 2013-04-16 14:20:35
回答 1查看 61关注 0票数 0

我想将表单中的数据存储到二维数组中,但似乎在将数据插入到数组中时出现了问题。如果我回显$orderArray,它似乎可以工作,但是如果我回显$orderArray1,就会出错

代码语言:javascript
复制
$dateArray = array();
$orderArray = array(array());
$amountArray = array(array());
$count = 0;
$count2 = 0;
foreach ($_POST['export'] as $date){ 
    $dateArray[$count] =  $date;
    include "/storescript/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT * FROM ordera WHERE orderDate = '$date' ORDER BY     orderid ASC");
    $productCount = mysql_num_rows($sql); // count the output amount
        if ($productCount > 0) {
            while($row = mysql_fetch_array($sql)){ 
                $orderArray[$count][$count2] = $row["orderAmount"];
                $amountArray[$count][$count2] = $row["itemAmount"];
                $count2++;

            }
        }
            $count++;
    }
EN

回答 1

Stack Overflow用户

发布于 2013-04-16 14:39:17

我会将代码简化为:

代码语言:javascript
复制
// connect here
include "/storescript/connect_to_mysql.php"; 

// make date list safe for querying
$dates = join(',', array_map(function($date) {
    return sprintf("'%s'", mysql_real_escape_string($date));
}, $_POST['export']);

// run query    
$sql = "SELECT * FROM ordera WHERE orderDate IN ($dates) ORDER BY orderid";
$res = mysql_query($sql) or die("Error in query");

// collect results
while ($row = mysql_fetch_array($res)) {
    $orders[$date][] = $row['orderAmount'];
    $amounts[$date][] = $row['itemAmount'];
}

// do stuff with results
foreach ($orders as $date => $orderAmounts) {
    print_r($orderAmounts);
    print_r($amounts[$date]);
}

另外,请了解PDO或mysqli;旧的mysql_函数已弃用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16030072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档