0

我的问题可能听起来有些笨拙,但这就是我想做的。我有一个$data从 SQL 查询中获得的名为的数组。供您参考,我在这里放的是前两个元素:

Array
(
    [0] => Array
        (
            [test_pack_id] => 9f27643023a83addd5eed41c4aade840
            [test_pack_name] => Bank Exams Complete Combo
            [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude + 2 Mega tests of all 3 subjects.

Total Tests in this Package : 26
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 399.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 1
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675
            [test_pack_created_date] => 1347127051
            [test_pack_updated_date] => 1349235701
            [test_pack_purchase_date] => 1351228594
        )

    [1] => Array
        (
            [test_pack_id] => e7e95de96987cc7c89c1f0183110fb38
            [test_pack_name] => Bank Reasoning
            [test_pack_desc] => This package contains 8 tests on Reasoning.
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => free
            [test_pack_price] => 0.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 4
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1347127196
            [test_pack_updated_date] => 1347127387
            [test_pack_purchase_date] => 1363775709
        )
)

现在我必须在这个数组中插入一个新的键值对。为此,我正在向数据库发出一个查询。将为 array 的每个元素触发查询$data。它的代码如下:

foreach($data as $data1) {  

            $sql  = " SELECT COUNT(*) as package_count FROM ". TBL_USER_PACKAGES. " WHERE pack_id ='".$data1['test_pack_id']."' AND pack_assign_date  ";  
            $sql .= " BETWEEN UNIX_TIMESTAMP(CURDATE()) and UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 day)) ";
            $this->mDb->Query( $sql);
            $data1['package_sold_count'] = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE);
}

但是当我访问这个查询的结果时,我得到的是这样的数组格式:

Array(
[0] => Array
        (
            [test_pack_id] => 9f27643023a83addd5eed41c4aade840
            [test_pack_name] => Bank Exams Complete Combo
            [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude + 2 Mega tests of all 3 subjects.

Total Tests in this Package : 26
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 399.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 1
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675
            [test_pack_created_date] => 303
            [test_pack_updated_date] => 1349235701
            [test_pack_purchase_date] => 256
            [package_sold_count] => Array
                (
                    [package_count] => 4
                )
[1] => Array
        (
            [test_pack_id] => e7e95de96987cc7c89c1f0183110fb38
            [test_pack_name] => Bank Reasoning
            [test_pack_desc] => This package contains 8 tests on Reasoning.
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => free
            [test_pack_price] => 0.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 4
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 303
            [test_pack_updated_date] => 1347127387
            [test_pack_purchase_date] => 110
            [package_sold_count] => Array
                (
                    [package_count] => 2
                )
)

实际上我希望数组作为一个新的键值对而不是一个新的关联数组,如下所示:

    Array(
    [0] => Array
            (
                [test_pack_id] => 9f27643023a83addd5eed41c4aade840
                [test_pack_name] => Bank Exams Complete Combo
                [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude + 2 Mega tests of all 3 subjects.

    Total Tests in this Package : 26
                [test_pack_type_id] => 3
                [test_pack_image] => 
                [test_pack_validity_year] => 0
                [test_pack_validity_month] => 3
                [test_pack_validity_days] => 0
                [test_pack_plan] => paid
                [test_pack_price] => 399.00
                [test_pack_no_tests] => 0
                [test_pack_publish] => yes
                [test_pack_sold] => 1
                [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675
                [test_pack_created_date] => 303
                [test_pack_updated_date] => 1349235701
                [test_pack_purchase_date] => 256
                [package_sold_count] => 4

)

谁能帮助我实现这一目标?提前致谢。

4

2 回答 2

1

当您在此行中获取新的键/值对时:

 $data1['package_sold_count'] = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE);

您实际上是在获取一个数组,即使它只是一个元素,这就是您所看到的。

相反,请执行以下操作:

$temp = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE);
$data1['package_sold_count'] = $temp['package_count']; // assumes associative array

这也可能有效,但我偶尔会遇到将代码打包得太紧的问题。

$data1['package_sold_count'] = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE)['package_count'];
于 2013-07-08T20:05:01.717 回答
1

像下面这样简单的东西应该可以正常工作,做你想做的事:

<?php
    foreach ($array as &$subArray) {
        $subArray["package_sold_count"] = $subArray["package_sold_count"]["package_count"];
    }
    unset($subArray);
?>

或者在分配时简单地设置它:

foreach($data as $data1) {  

            $sql  = " SELECT COUNT(*) as package_count FROM ". TBL_USER_PACKAGES. " WHERE pack_id ='".$data1['test_pack_id']."' AND pack_assign_date  ";  
            $sql .= " BETWEEN UNIX_TIMESTAMP(CURDATE()) and UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 day)) ";
            $this->mDb->Query( $sql);

            //First assign a variable
            $dbFetch = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE); 

            //Then refer to it
            $data1['package_sold_count'] = $dbFetch["package_count"]; 
}
于 2013-07-08T20:00:25.173 回答