如何从多维数组中访问值并在循环中触发查询?

时间:2013-07-09 13:37:50

标签: php sql arrays join associative-array

我有一个名为$user_study_test_packages的多维数组。作为参考,我正在打印整个阵列。

Array
(
    [test] => Array
        (
            [0] => Array
                (
                    [user_pack_id] => 214
                    [user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [pack_id] => 21e86b3ebf6a8af2a9fcf136c4f8e88a
                    [pack_name] => Section wise test
                    [pack_price_discounted] => 0.00
                    [pack_discount] => 0.00
                    [pack_price] => 0.00
                    [pack_type] => test
                    [pack_expiry_date] => 21st March 2014
                    [transaction_no] => 
                    [pack_assign_type] => online_purchase
                    [pack_assign_date] => 0
                    [transaction_id] => 
                    [transaction_cc_avenue_no] => 
                    [transaction_card_category] => 
                    [transaction_user_id] => 
                    [transaction_user_name] => 
                    [transaction_user_email_id] => 
                    [transaction_deal_code] => 
                    [transaction_dc_id] => 
                    [transaction_amount] => 
                    [transaction_discount] => 
                    [transaction_total_amount] => 
                    [transaction_data_assign] => 
                    [transaction_status] => 
                    [transaction_date] => 
                    [transaction_update_date] => 
                    [transaction_update_user_id] => 
                    [pack_expiry_date1] => 2014-03-21
                    [test_pack_desc] => sectionm wise tests
                )

            [1] => Array
                (
                    [user_pack_id] => 208
                    [user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [pack_id] => e7e95de96987cc7c89c1f0183110fb38
                    [pack_name] => Bank Reasoning
                    [pack_price_discounted] => 0.00
                    [pack_discount] => 0.00
                    [pack_price] => 0.00
                    [pack_type] => test
                    [pack_expiry_date] => 19th June 2013
                    [transaction_no] => 
                    [pack_assign_type] => online_purchase
                    [pack_assign_date] => 0
                    [transaction_id] => 
                    [transaction_cc_avenue_no] => 
                    [transaction_card_category] => 
                    [transaction_user_id] => 
                    [transaction_user_name] => 
                    [transaction_user_email_id] => 
                    [transaction_deal_code] => 
                    [transaction_dc_id] => 
                    [transaction_amount] => 
                    [transaction_discount] => 
                    [transaction_total_amount] => 
                    [transaction_data_assign] => 
                    [transaction_status] => 
                    [transaction_date] => 
                    [transaction_update_date] => 
                    [transaction_update_user_id] => 
                    [pack_expiry_date1] => 2013-06-19
                    [test_pack_desc] => This package contains 8 tests on Reasoning.
                )

            [2] => Array
                (
                    [user_pack_id] => 207
                    [user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [pack_id] => 42eaca88ae0079a77604d75d30846e46
                    [pack_name] => test question order
                    [pack_price_discounted] => 0.00
                    [pack_discount] => 0.00
                    [pack_price] => 0.00
                    [pack_type] => test
                    [pack_expiry_date] => 19th April 2013
                    [transaction_no] => 
                    [pack_assign_type] => online_purchase
                    [pack_assign_date] => 0
                    [transaction_id] => 
                    [transaction_cc_avenue_no] => 
                    [transaction_card_category] => 
                    [transaction_user_id] => 
                    [transaction_user_name] => 
                    [transaction_user_email_id] => 
                    [transaction_deal_code] => 
                    [transaction_dc_id] => 
                    [transaction_amount] => 
                    [transaction_discount] => 
                    [transaction_total_amount] => 
                    [transaction_data_assign] => 
                    [transaction_status] => 
                    [transaction_date] => 
                    [transaction_update_date] => 
                    [transaction_update_user_id] => 
                    [pack_expiry_date1] => 2013-04-19
                    [test_pack_desc] => test question order
                )

            [3] => Array
                (
                    [user_pack_id] => 206
                    [user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [pack_id] => fc55acb1edc47d2dc339562db4c13729
                    [pack_name] => test question order1
                    [pack_price_discounted] => 0.00
                    [pack_discount] => 0.00
                    [pack_price] => 0.00
                    [pack_type] => test
                    [pack_expiry_date] => 19th April 2013
                    [transaction_no] => 
                    [pack_assign_type] => online_purchase
                    [pack_assign_date] => 0
                    [transaction_id] => 
                    [transaction_cc_avenue_no] => 
                    [transaction_card_category] => 
                    [transaction_user_id] => 
                    [transaction_user_name] => 
                    [transaction_user_email_id] => 
                    [transaction_deal_code] => 
                    [transaction_dc_id] => 
                    [transaction_amount] => 
                    [transaction_discount] => 
                    [transaction_total_amount] => 
                    [transaction_data_assign] => 
                    [transaction_status] => 
                    [transaction_date] => 
                    [transaction_update_date] => 
                    [transaction_update_user_id] => 
                    [pack_expiry_date1] => 2013-04-19
                    [test_pack_desc] => 111
                )

            [4] => Array
                (
                    [user_pack_id] => 202
                    [user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [pack_id] => 9f27643023a83addd5eed41c4aade840
                    [pack_name] => Bank Exams Complete Combo
                    [pack_price_discounted] => 399.00
                    [pack_discount] => 0.00
                    [pack_price] => 399.00
                    [pack_type] => test
                    [pack_expiry_date] => 26th January 2013
                    [transaction_no] => qfAcXhptywH2nxk
                    [pack_assign_type] => online_purchase
                    [pack_assign_date] => 1373337030
                    [transaction_id] => 97
                    [transaction_cc_avenue_no] => CCA12JCTU584
                    [transaction_card_category] => NETBANKING
                    [transaction_user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [transaction_user_name] => Amol Joshi
                    [transaction_user_email_id] => amol.joshi@creatywa.com
                    [transaction_deal_code] => 
                    [transaction_dc_id] => 0
                    [transaction_amount] => 399.00
                    [transaction_discount] => 0.00
                    [transaction_total_amount] => 399.00
                    [transaction_data_assign] => 1
                    [transaction_status] => success
                    [transaction_date] => 1351228593
                    [transaction_update_date] => 0
                    [transaction_update_user_id] => 
                    [pack_expiry_date1] => 2013-01-26
                    [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
                )

            [5] => Array
                (
                    [user_pack_id] => 201
                    [user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [pack_id] => 5810758e0ee945090506e02c35222f0e
                    [pack_name] => Bank Quantitative Aptitude
                    [pack_price_discounted] => 149.00
                    [pack_discount] => 0.00
                    [pack_price] => 149.00
                    [pack_type] => test
                    [pack_expiry_date] => 22nd January 2013
                    [transaction_no] => HhmfwX4q7yvx5nd
                    [pack_assign_type] => online_purchase
                    [pack_assign_date] => 0
                    [transaction_id] => 96
                    [transaction_cc_avenue_no] => CCA12JCTL532
                    [transaction_card_category] => NETBANKING
                    [transaction_user_id] => 5291d6ee6c5b577514e832f077a496ca
                    [transaction_user_name] => Amol Joshi
                    [transaction_user_email_id] => amol.joshi@creatywa.com
                    [transaction_deal_code] => 
                    [transaction_dc_id] => 0
                    [transaction_amount] => 149.00
                    [transaction_discount] => 0.00
                    [transaction_total_amount] => 149.00
                    [transaction_data_assign] => 1
                    [transaction_status] => success
                    [transaction_date] => 1350890908
                    [transaction_update_date] => 0
                    [transaction_update_user_id] => 
                    [pack_expiry_date1] => 2013-01-22
                    [test_pack_desc] =>  This package contains 8 tests on Bank Quantitative Aptitude.
                )

        )

)

现在我想从内部数组发出每个[pack_id]的SQL查询。一个[pack_id]值的演示SQL查询如下:

SELECT package_type.package_type_name
FROM package_type
JOIN test_packages ON package_type.package_type_id = test_packages.test_pack_type_id
WHERE test_packages.test_pack_id = '21e86b3ebf6a8af2a9fcf136c4f8e88a'

此查询返回所需的结果(即package_name)。 实际上我想从上面的数组中为每个[pack_id]值触发此查询,并生成一个以key =>值格式为[pack_id] => 'package_type_name'的关联数组 我该怎么做到这一点?任何人都可以帮我解决这个问题吗?提前致谢。

1 个答案:

答案 0 :(得分:1)

你能尝试这样的事吗?

$arrResult = array();

foreach( $user_study_test_packages['test'] as $innerArray)
{
    $sql = 'SELECT... WHERE test_packages.test_pack_id = ' . $innerArray['pack_id'];

    $queryResult = // call you query

    $arrResult[$innerArray['pack_id']] = $queryResult['package_type_name'];
}