php - How to fetch specific elements from an array and create a new array based on the comparison of an array key's value? -
my question may sound confusing people. in fact it's quite simple one. let me clear scenario. i've array titled $test_result_data
follows:
array ( [0] => array ( [test_pack_id] => 8ed32f6479a0169db3531d3366996d35 [test_pack_name] => cpt free samples [test_pack_desc] => package contains free sample test of 30 minutes containing 30 questions - fundamentals of accounting, mercantile law, general economics , quantitative aptitude. perfect discover 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] => package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each , <b>3 full syllabus tests</b> of 1 hour covering syllabus of cpt general economics.<br> <b>total tests in 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 - 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 - 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 - 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] => ) ) ) )
now can see array has 2 main arrays inside it. each of these arrays contain test package data. array creates dynamically can't how many items each time contain. i'm haing 1 $test_pack_id
, want fetch array elements above array value of key [test_pack_id] mathes value of variable $test_pack_id
. want create whole new array these matching test package data items. can please me in getting array?
suppose have following variable:
$test_pack_id = '84880f321fc957f9b28b702d5a43f57b';//this input
the array $test_result_data
can contain id in it's elements 1 time, more 1 time or doesn't contain. if match found should return matching elements. in above case output shoud follows:
array ( [0] => array ( [test_pack_id] => 84880f321fc957f9b28b702d5a43f57b [test_pack_name] => cpt general economics [test_pack_desc] => package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each , <b>3 full syllabus tests</b> of 1 hour covering syllabus of cpt general economics.<br> <b>total tests in 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 ) ) ) )
use array_filter:
$filtered = array_filter($yourarray, function($element) use($test_pack_id) { return $element['test_pack_id'] == $test_pack_id; });
Comments
Post a Comment