我的問題聽起來有些笨拙,但這正是我想要做的。 我有一個從SQL查詢獲得的名爲$data
的數組。爲了您的參考,我把它放在前面兩個元素:如何將數組元素轉換爲PHP中的simplae元素?
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
)
)
現在我要在這個數組中插入一個新的鍵值對。爲此,我向數據庫發出一個查詢。該查詢將針對數組$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
)
任何人都可以幫助我實現這個目標嗎?提前致謝。