【问题标题】:How to fetch specific elements from an array and create a new array based on the comparison of an array key's value?如何从数组中获取特定元素并根据数组键值的比较创建一个新数组?
【发布时间】:2014-04-27 01:12:20
【问题描述】:

我的问题可能会让某些人感到困惑。但实际上这是一个非常简单的。让我给你解释一下场景。我有一个名为$test_result_data 的数组,如下所示:

Array
(
    [0] => Array
        (
            [test_pack_id] => 8ed32f6479a0169db3531d3366996d35
            [test_pack_name] => CPT Free Samples
            [test_pack_desc] => This package contains a free sample test of 30 minutes containing 30 questions from - Fundamentals of accounting, Mercantile Law, General Economics and Quantitative Aptitude. This is perfect to discover the beauty of online exam preparation.
            [test_pack_type_id] => 7
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 0
            [test_pack_validity_days] => 3
            [test_pack_plan] => free
            [test_pack_price] => 0.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_code] => 
            [test_pack_sold] => 22
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1337014879
            [test_pack_updated_date] => 1376886700
            [test_pack_purchase_date] => 1364454118
            [test_details] => Array
                (
                    [0] => Array
                        (
                            [test_id] => 159
                            [test_name] => CPT Sample Test
                            [test_max_score] => 120.000
                            [test_no_questions] => 30
                            [test_attempt] => 15
                            [test_not_attempt] => 7
                            [avg_test_user_time_used] => 00:04:39 Hr
                            [top_score] => 46.00
                            [avg_score] => 3
                            [lower_score] => 0.00
                        )

                )

        )

    [1] => Array
        (
            [test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
            [test_pack_name] => CPT General Economics
            [test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
<b>Total Tests in this Package : 27</b>
            [test_pack_type_id] => 7
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 6
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 359.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_code] => 
            [test_pack_sold] => 0
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1338298119
            [test_pack_updated_date] => 1343564534
            [test_pack_purchase_date] => 0
            [test_details] => Array
                (
                    [0] => Array
                        (
                            [test_id] => 580
                            [test_name] => CPT Testlet : Demand & Supply 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [1] => Array
                        (
                            [test_id] => 581
                            [test_name] => CPT Test : Micro Economics
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [2] => Array
                        (
                            [test_id] => 583
                            [test_name] => CPT Testlet : Micro Economics 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [3] => Array
                        (
                            [test_id] => 582
                            [test_name] => CPT Testlet : Micro Economics 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [4] => Array
                        (
                            [test_id] => 579
                            [test_name] => CPT Testlet : Demand & Supply 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [5] => Array
                        (
                            [test_id] => 578
                            [test_name] => CPT Test : Demand & Supply
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [6] => Array
                        (
                            [test_id] => 577
                            [test_name] => CPT Testlet : Production & Cost 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [7] => Array
                        (
                            [test_id] => 576
                            [test_name] => CPT Testlet : Production & Cost 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [8] => Array
                        (
                            [test_id] => 575
                            [test_name] => CPT Test : Production & Cost
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [9] => Array
                        (
                            [test_id] => 574
                            [test_name] => CPT Testlet : Price Determination 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [10] => Array
                        (
                            [test_id] => 573
                            [test_name] => CPT Testlet : Price Determination 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [11] => Array
                        (
                            [test_id] => 572
                            [test_name] => CPT Test : Price Determination
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [12] => Array
                        (
                            [test_id] => 571
                            [test_name] => CPT Testlet : Indian Economy - A Profile 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [13] => Array
                        (
                            [test_id] => 570
                            [test_name] => CPT Testlet : Indian Economy - A Profile 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [14] => Array
                        (
                            [test_id] => 569
                            [test_name] => CPT Test : Indian Economy - A Profile
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [15] => Array
                        (
                            [test_id] => 568
                            [test_name] => CPT Testlet : Aspects of Indian Economy 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [16] => Array
                        (
                            [test_id] => 567
                            [test_name] => CPT Testlet : Aspects of Indian Economy 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [17] => Array
                        (
                            [test_id] => 566
                            [test_name] => CPT Test : Aspects of Indian Economy
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [18] => Array
                        (
                            [test_id] => 565
                            [test_name] => CPT Testlet : Economic Reforms in India 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [19] => Array
                        (
                            [test_id] => 564
                            [test_name] => CPT Testlet : Economic Reforms in India 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [20] => Array
                        (
                            [test_id] => 563
                            [test_name] => CPT Test : Economic Reforms in India
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [21] => Array
                        (
                            [test_id] => 562
                            [test_name] => CPT Testlet : Money & Banking 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [22] => Array
                        (
                            [test_id] => 561
                            [test_name] => CPT Testlet : Money & Banking 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [23] => Array
                        (
                            [test_id] => 560
                            [test_name] => CPT Test : Money & Banking
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [24] => Array
                        (
                            [test_id] => 559
                            [test_name] => CPT Test : General Economics 1
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [25] => Array
                        (
                            [test_id] => 558
                            [test_name] => CPT Test : General Economics 2
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [26] => Array
                        (
                            [test_id] => 557
                            [test_name] => CPT Test : General Economics 3
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                )

        )

)

现在你可以看到这个数组里面有两个主数组。这些数组中的每一个都包含测试包数据。这个数组是动态创建的,所以我们不能说每次它包含多少个项目。现在我有一个$test_pack_id,我只想从上面的数组中获取那些数组元素,其中键[test_pack_id] 的值与变量$test_pack_id 的值相匹配。我想为这些匹配的测试包数据项创建一个全新的数组。任何人都可以帮我获得这个数组吗?

假设我有以下变量:

$test_pack_id = '84880f321fc957f9b28b702d5a43f57b';//This is my input

数组$test_result_data 可以在其元素中包含此id 一次、多次或不包含。但是如果找到匹配,那么它应该返回所有匹配的元素。在上述情况下,输出应如下所示:

Array
(
    [0] => Array
        (
[test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
                [test_pack_name] => CPT General Economics
                [test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
    <b>Total Tests in this Package : 27</b>
                [test_pack_type_id] => 7
                [test_pack_image] => 
                [test_pack_validity_year] => 0
                [test_pack_validity_month] => 6
                [test_pack_validity_days] => 0
                [test_pack_plan] => paid
                [test_pack_price] => 359.00
                [test_pack_no_tests] => 0
                [test_pack_publish] => yes
                [test_pack_code] => 
                [test_pack_sold] => 0
                [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_created_date] => 1338298119
                [test_pack_updated_date] => 1343564534
                [test_pack_purchase_date] => 0
                [test_details] => Array
                    (
                        [0] => Array
                            (
                                [test_id] => 580
                                [test_name] => CPT Testlet : Demand & Supply 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [1] => Array
                            (
                                [test_id] => 581
                                [test_name] => CPT Test : Micro Economics
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [2] => Array
                            (
                                [test_id] => 583
                                [test_name] => CPT Testlet : Micro Economics 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [3] => Array
                            (
                                [test_id] => 582
                                [test_name] => CPT Testlet : Micro Economics 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )

                        [4] => Array
                            (
                                [test_id] => 579
                                [test_name] => CPT Testlet : Demand & Supply 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [5] => Array
                            (
                                [test_id] => 578
                                [test_name] => CPT Test : Demand & Supply
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )




                    )

            )

    )

【问题讨论】:

    标签: php arrays associative-array key-value


    【解决方案1】:

    使用array_filter:

    $filtered = array_filter($yourArray, function($element) use($test_pack_id) {
      return $element['test_pack_id'] == $test_pack_id;
    });
    

    【讨论】:

    • 但它是从 1 开始索引。我希望它从 0 开始索引。你能相应地修改你的答案吗?
    • @PHPLover 尝试添加 $filtered = array_values($filtered);
    • 但是在哪里?您能否修改您的答案以获得此更改?
    【解决方案2】:

    简答:

    function packed_element($id) {
    foreach($test_result_data as $k => $v)
      if ($v->test_pack_id === $id)
        return $v;
    }
    

    但是,如果您可以管理生成数组的代码,我建议您简单地使用字典数组而不是简单的字典数组,以您的 id 作为键。因此,您可以直接引用您的元素。例如,您可以生成这样的数组:

    Array
    (
        [8ed32f6479a0169db3531d3366996d35] => Array
            (
                [test_pack_id] => 8ed32f6479a0169db3531d3366996d35
                [test_pack_name] => CPT Free Samples
                [test_pack_desc] => This package contains a free sample test of 30 minutes containing 30 questions from - Fundamentals of accounting, Mercantile Law, General Economics and Quantitative Aptitude. This is perfect to discover the beauty of online exam preparation.
                [test_pack_type_id] => 7
                [test_pack_image] => 
                [test_pack_validity_year] => 0
                [test_pack_validity_month] => 0
                [test_pack_validity_days] => 3
                [test_pack_plan] => free
                [test_pack_price] => 0.00
                [test_pack_no_tests] => 0
                [test_pack_publish] => yes
                [test_pack_code] => 
                [test_pack_sold] => 22
                [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_created_date] => 1337014879
                [test_pack_updated_date] => 1376886700
                [test_pack_purchase_date] => 1364454118
                [test_details] => Array
                    (
                        [0] => Array
                            (
                                [test_id] => 159
                                [test_name] => CPT Sample Test
                                [test_max_score] => 120.000
                                [test_no_questions] => 30
                                [test_attempt] => 15
                                [test_not_attempt] => 7
                                [avg_test_user_time_used] => 00:04:39 Hr
                                [top_score] => 46.00
                                [avg_score] => 3
                                [lower_score] => 0.00
                            )
    
                    )
    
            )
    
        [84880f321fc957f9b28b702d5a43f57b] => Array
            (
                [test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
                [test_pack_name] => CPT General Economics
                [test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
    <b>Total Tests in this Package : 27</b>
                [test_pack_type_id] => 7
                [test_pack_image] => 
                [test_pack_validity_year] => 0
                [test_pack_validity_month] => 6
                [test_pack_validity_days] => 0
                [test_pack_plan] => paid
                [test_pack_price] => 359.00
                [test_pack_no_tests] => 0
                [test_pack_publish] => yes
                [test_pack_code] => 
                [test_pack_sold] => 0
                [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_created_date] => 1338298119
                [test_pack_updated_date] => 1343564534
                [test_pack_purchase_date] => 0
                [test_details] => Array
                    (
                        [0] => Array
                            (
                                [test_id] => 580
                                [test_name] => CPT Testlet : Demand & Supply 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [1] => Array
                            (
                                [test_id] => 581
                                [test_name] => CPT Test : Micro Economics
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [2] => Array
                            (
                                [test_id] => 583
                                [test_name] => CPT Testlet : Micro Economics 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [3] => Array
                            (
                                [test_id] => 582
                                [test_name] => CPT Testlet : Micro Economics 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [4] => Array
                            (
                                [test_id] => 579
                                [test_name] => CPT Testlet : Demand & Supply 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [5] => Array
                            (
                                [test_id] => 578
                                [test_name] => CPT Test : Demand & Supply
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [6] => Array
                            (
                                [test_id] => 577
                                [test_name] => CPT Testlet : Production & Cost 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [7] => Array
                            (
                                [test_id] => 576
                                [test_name] => CPT Testlet : Production & Cost 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [8] => Array
                            (
                                [test_id] => 575
                                [test_name] => CPT Test : Production & Cost
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [9] => Array
                            (
                                [test_id] => 574
                                [test_name] => CPT Testlet : Price Determination 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [10] => Array
                            (
                                [test_id] => 573
                                [test_name] => CPT Testlet : Price Determination 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [11] => Array
                            (
                                [test_id] => 572
                                [test_name] => CPT Test : Price Determination
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [12] => Array
                            (
                                [test_id] => 571
                                [test_name] => CPT Testlet : Indian Economy - A Profile 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [13] => Array
                            (
                                [test_id] => 570
                                [test_name] => CPT Testlet : Indian Economy - A Profile 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [14] => Array
                            (
                                [test_id] => 569
                                [test_name] => CPT Test : Indian Economy - A Profile
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [15] => Array
                            (
                                [test_id] => 568
                                [test_name] => CPT Testlet : Aspects of Indian Economy 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [16] => Array
                            (
                                [test_id] => 567
                                [test_name] => CPT Testlet : Aspects of Indian Economy 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [17] => Array
                            (
                                [test_id] => 566
                                [test_name] => CPT Test : Aspects of Indian Economy
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [18] => Array
                            (
                                [test_id] => 565
                                [test_name] => CPT Testlet : Economic Reforms in India 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [19] => Array
                            (
                                [test_id] => 564
                                [test_name] => CPT Testlet : Economic Reforms in India 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [20] => Array
                            (
                                [test_id] => 563
                                [test_name] => CPT Test : Economic Reforms in India
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [21] => Array
                            (
                                [test_id] => 562
                                [test_name] => CPT Testlet : Money & Banking 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [22] => Array
                            (
                                [test_id] => 561
                                [test_name] => CPT Testlet : Money & Banking 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [23] => Array
                            (
                                [test_id] => 560
                                [test_name] => CPT Test : Money & Banking
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [24] => Array
                            (
                                [test_id] => 559
                                [test_name] => CPT Test : General Economics 1
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                        [25] => Array
                            (
                                [test_id] => 558
                                [test_name] => CPT Test : General Economics 2
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )
    
                        [26] => Array
                            (
                                [test_id] => 557
                                [test_name] => CPT Test : General Economics 3
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )
    
                    )
    
            )
    
    )
    

    然后你可以简单地这样做:

    $my_packed_data = $test_result_data[84880f321fc957f9b28b702d5a43f57b];

    【讨论】:

    • 您不能将此数组用作对象。
    • 为什么不能用这个作为对象?如果我这样做: $object = (object) $my_packed_data;我认为这是工作!
    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多