0

私の質問はやや不器用に聞こえるかもしれませんが、それが私がやりたかったことです。$dataSQLクエリから取得したという名前の配列があります。ご参考までに、最初の 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] => 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
        )
)

ここで、この配列に新しいキーと値のペアを 1 つ挿入する必要があります。そのために、データベースに対して 1 つのクエリを実行しています。クエリは、 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);

要素が 1 つしかない場合でも、実際には配列をフェッチしているので、それが表示されます。

代わりに、次のようにします。

$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 に答える