How to fetch specific elements from an array and create a new array based on the comparison of an array key's value?

Course Queries Syllabus Queries . 2 years ago

  0   2   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (2)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


My question may sound confusing to some people. But in fact it's a quite simple one. Let me clear you the scenario. I've an array titled $test_result_data as follows:

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
                                                    
                                                    
0 views   0 shares
profilepic.png
manpreet 2 years ago


Short answer:

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

But if you can manage the code that generate the array I suggest you to simply use dictionary array over simple one, that have your id as key. So you can referrer to your element directly. For example you can generate an array like this:

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
                        )

                )

        
                                                            
                                                            
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community