I am having two arrays $exam_questions and $attempted_responses. I want to compare attempted_responses['question_id'] object to $exam_questions['id'] if they match.
output of both arrays are:
$exam_questions
Array
(
[0] => stdClass Object
(
[id] => 747
[section_id] =>
[text] => What does HTML stand for?
[exam_id] => 83
[date_created] => 2019-11-27 09:41:41
[answered] => 1
)
[1] => stdClass Object
(
[id] => 748
[section_id] =>
[text] => Who is making the Web standards?
[exam_id] => 83
[date_created] => 2019-11-27 09:43:04
[answered] => 1
)
[2] => stdClass Object
(
[id] => 749
[section_id] =>
[text] => What does CSS stand for?
[exam_id] => 83
[date_created] => 2019-11-27 09:43:51
[answered] => 1
)
[3] => stdClass Object
(
[id] => 750
[section_id] =>
[text] => Which is the correct CSS syntax?
[exam_id] => 83
[date_created] => 2019-11-27 09:45:17
[answered] => 1
)
[4] => stdClass Object
(
[id] => 751
[section_id] =>
[text] => How do you insert a comment in a CSS file?
[exam_id] => 83
[date_created] => 2019-11-27 09:46:32
[answered] => 1
)
[5] => stdClass Object
(
[id] => 752
[section_id] =>
[text] => What is the correct HTML for inserting an image?
[exam_id] => 83
[date_created] => 2019-11-27 09:47:56
[answered] => 1
)
[6] => stdClass Object
(
[id] => 753
[section_id] =>
[text] => How can you make a list that lists the items with numbers?
[exam_id] => 83
[date_created] => 2019-11-27 09:48:50
[answered] => 1
)
[7] => stdClass Object
(
[id] => 754
[section_id] =>
[text] => What is the correct HTML for creating a hyperlink?
[exam_id] => 83
[date_created] => 2019-11-27 09:49:45
[answered] => 1
)
)
$attempted_responses
Array
(
[0] => Array
(
[question_id] => 747
[answer_id] => 2641
)
[1] => Array
(
[question_id] => 748
[answer_id] => 2646
)
[2] => Array
(
[question_id] => 749
[answer_id] => 2650
)
[3] => Array
(
[question_id] => 750
[answer_id] => 2654
)
[4] => Array
(
[question_id] => 751
[answer_id] => 2656
)
[5] => Array
(
[question_id] => 752
[answer_id] => 2661
)
[6] => Array
(
[question_id] => 753
[answer_id] => 2663
)
[7] => Array
(
[question_id] => 754
[answer_id] => 2668
)
)
foreach($exam_questions as $exam_question){
foreach($attempted_responses as $attempted_response){
if($exam_question->id === $attempted_response['question_id']){
// your code
}
}
}
or your convert $exam_questions as a nested array.
$exam_questions_array = json_decode(json_encode($exam_questions), true);
then it can treated as a nested array.
foreach($exam_questions_array as $exam_question){
foreach($attempted_responses as $attempted_response){
if($exam_question['id'] === $attempted_response['question_id']){
// your code
}
}
}
Related
I'm using Laravel and I wonder how can I put this array in a foreach? for read it
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 0001 [type] => Item [attributes] => stdClass Object ( [product_name] => ST Slotted Sport Brake Rotors [part_number] => st126.39027SL [mfr_part_number] => 126.39027SL [part_description] => StopTech Power Slot 00-04 Volvo S40/V40 Front Left Slotted Rotor [category] => Brakes, Rotors & Pads [subcategory] => Brake Rotors - Slotted [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 15 [width] => 15 [height] => 4 [weight] => 12 ) ) [brand_id] => 56 [brand] => Stoptech [price_group_id] => 129 [price_group] => Stoptech [active] => 1 [regular_stock] => [dropship_controller_id] => 19 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d5otzd52uv6zz.cloudfront.net/fa33cfb5-0c91-47de-9a49-67f4175a5bff-100.jpg [barcode] => 805890204282 ) ) [1] => stdClass Object ( [id] => 078595 [type] => Item [attributes] => stdClass Object ( [product_name] => RAD Clutch Fork Stop [part_number] => rad20-0262 [mfr_part_number] => 20-0262 [part_description] => Radium Engineering Mitsubishi Evo 8-10 Clutch Fork Stop [category] => Drivetrain [subcategory] => Clutch Uncategorized [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 4.75 [width] => 4.5 [height] => 4.5 [weight] => 0.5 ) ) [brand_id] => 148 [brand] => Radium Engineering [price_group_id] => 406 [price_group] => Radium Engineering [active] => 1 [regular_stock] => 1 [dropship_controller_id] => 81 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d32vzsop7y1h3k.cloudfront.net/adb8244ed2d562e078c5ce2928750f2a.JPG ) ) [2] => stdClass Object ( [id] => 095906 [type] => Item [attributes] => stdClass Object ( [product_name] => MLR Trans Rebuild Kits [part_number] => mlr88075K [mfr_part_number] => 88075K [part_description] => McLeod Performance Transmission Rebuild Kit w/ Kolene Steels 4L80E 1997-2011 - Stage 1 [category] => Drivetrain [subcategory] => Transmission Rebuild Kits [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 24 [width] => 16 [height] => 5.25 [weight] => 14.75 ) ) [brand_id] => 169 [brand] => McLeod Racing [price_group_id] => 456 [price_group] => McLeod Racing [active] => 1 [regular_stock] => [dropship_controller_id] => 116 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) ) ) [3] => stdClass Object ( [id] => 1 [type] => Item [attributes] => stdClass Object ( [product_name] => TXS Boost Controllers [part_number] => txs-BC-HPBC [mfr_part_number] => txs-BC-HPBC [part_description] => Turbo XS High Performance Boost Controller [category] => Forced Induction [subcategory] => Boost Controllers [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 4.75 [width] => 3.25 [height] => 2.25 [weight] => 0.5 ) ) [brand_id] => 63 [brand] => Turbo XS [price_group_id] => 130 [price_group] => Turbo XS [active] => 1 [regular_stock] => 1 [dropship_controller_id] => 0 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d5otzd52uv6zz.cloudfront.net/92670bde-a650-4ae6-a53e-0da8b2a26d1c-100.jpg [barcode] => 053176487038 ) )
Thanks!
Just loop through your data with
foreach($yourvar->data as $key=> $data){
echo $data->id;
}
If you get an StdClass object it means you're iterating over an object through -> if you've an array you can print through $data['key']
Array
(
[records] => Array
(
[0] => stdClass Object
(
[id] => 6
[fname] => hardik
[type] => Active
[rid] => 1
[rname] => Principle
[uid] => 48
)
[1] => stdClass Object
(
[id] => 12
[fname] => gautam
[type] => De-Active
[rid] => 1
[rname] => Principle
[uid] => 54
)
[2] => stdClass Object
(
[id] => 10
[fname] => burhan
[type] => Active
[rid] => 2
[rname] => Teacher
[uid] => 52
)
[3] => stdClass Object
(
[id] => 13
[fname] => gautam
[type] => De-Active
[rid] => 2
[rname] => Teacher
[uid] => 54
)
[4] => stdClass Object
(
[id] => 7
[fname] => abc
[password] => d41d8cd98f00b204e9800998ecf8427e
[type] => Active
[rid] => 3
[rname] => Parent
[uid] => 49
)
)
)
How can I compare associative array in Codeigniter for remove duplicate user?
Hi my zend paginator works well when the $business_list array format is like below.
but when the array format changes it displays only one page instead of many pages.
$paginator = Zend_Paginator::factory($business_list);
Array
(
[0] => Array
(
[id] => 216
[userid] => 141
[title] => first req
[image] =>
[logo] =>
[description] =>
this is the first requirment
[date] => 2012-06-12 10:31:01
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 315
[business_id] => 216
[section_id] => 1
[subsection_id] => 13
[description] =>
)
[1] => Array
(
[id] => 316
[business_id] => 216
[section_id] => 3
[subsection_id] => 14
[description] =>
)
[2] => Array
(
[id] => 317
[business_id] => 216
[section_id] => 4
[subsection_id] => 15
[description] =>
)
)
[CmUser] => Array
(
[id] => 141
[username] => venki
[password] =>
[firstname] => venkatesh
[lastname] => abus
[image] => 54winter.jpg
[email] => xxx#xx.com
[phone] => 23423452
[group_id] => 2
[status] =>
[registered_date] => 2012-06-04 06:32:58
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
[1] => Array
(
[id] => 214
[userid] => 98
[title] => gopicontractor
[image] => 54bluehills.jpg
[logo] => 239waterlilies.jpg
[description] =>
TIt uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
[date] => 2012-06-11 12:18:58
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 305
[business_id] => 214
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 306
[business_id] => 214
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 307
[business_id] => 214
[section_id] => 3
[subsection_id] => 1
[description] =>
)
[3] => Array
(
[id] => 308
[business_id] => 214
[section_id] => 4
[subsection_id] => 9
[description] =>
)
)
[CmUser] => Array
(
[id] => 98
[username] => gopi.s
[password] =>
[firstname] => venkatesh
[lastname] => franc
[image] =>
[email] => ss#ss.com
[phone] => 23423452
[group_id] => 3
[status] =>
[registered_date] => 2012-05-16 12:36:57
[last_visit] => 0000-00-00 00:00:00
[is_active] => 1
[subscribe] => 1
)
)
)
the above array format displays paginatin correctly.
This is the array that displays only one page in pagination.
Zend_Paginator Object
(
[_cacheEnabled:protected] => 1
[_adapter:protected] => Zend_Paginator_Adapter_Array Object
(
[_array:protected] => Array
(
[0] => Array
(
[id] => 89
[username] => xx
[password] => xx
[firstname] => xx
[lastname] => xx
[image] =>
[email] => xx#ymail.com
[phone] => 2342345
[group_id] => 2
[status] => offline
[registered_date] => 2012-05-15 10:58:53
[last_visit] =>
[is_active] => 1
[subscribe] => 0
[CmBusiness] => Array
(
[0] => Array
(
[id] => 204
[userid] => 89
[title] => xxhousing
[image] => 760067.jpg
[logo] => xx_818f3c97e6_o.jpg
[description] => xx
[date] => 2012-05-31 13:36:17
[area] => 1
[budget] => 1
[type] => 1
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 251
[business_id] => 204
[section_id] => 1
[subsection_id] => 6
[description] => xx
)
[1] => Array
(
[id] => 252
[business_id] => 204
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[2] => Array
(
[id] => 253
[business_id] => 204
[section_id] => 3
[subsection_id] => 4
[description] => xx
)
[3] => Array
(
[id] => 254
[business_id] => 204
[section_id] => 4
[subsection_id] => 9
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 1
[type] => Personal business
[status] => 1
)
)
[1] => Array
(
[id] => 205
[userid] => 89
[title] => xx
[image] => 41217850-desktop-wallpapers-new-windows-xp.jpg
[logo] => 356anger_n.jpg
[description] => xx
[date] => 2012-05-31 13:37:15
[area] => 1
[budget] => 1
[type] => 3
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 255
[business_id] => 205
[section_id] => 1
[subsection_id] => 6
[description] =>xx
)
[1] => Array
(
[id] => 256
[business_id] => 205
[section_id] => 3
[subsection_id] => 1
[description] => xx
)
[2] => Array
(
[id] => 257
[business_id] => 205
[section_id] => 3
[subsection_id] => 2
[description] => xx
)
[3] => Array
(
[id] => 258
[business_id] => 205
[section_id] => 4
[subsection_id] => 10
[description] => xx
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 3
[type] => Bilde companies
[status] => 1
)
)
[2] => Array
(
[id] => 206
[userid] => 89
[title] => Nuestros recursos
[image] =>
[logo] =>
[description] => xx
[date] => 2012-05-31 13:38:04
[area] => 1
[budget] => 1
[type] => 4
[status] => 1
[CmAmount] => Array
(
[id] => 1
[amount] => 1000-1500
[status] => 1
)
[CmBusinessSection] => Array
(
[0] => Array
(
[id] => 259
[business_id] => 206
[section_id] => 1
[subsection_id] => 5
[description] =>
)
[1] => Array
(
[id] => 260
[business_id] => 206
[section_id] => 1
[subsection_id] => 6
[description] =>
)
[2] => Array
(
[id] => 261
[business_id] => 206
[section_id] => 3
[subsection_id] => 2
[description] =>
)
[3] => Array
(
[id] => 262
[business_id] => 206
[section_id] => 4
[subsection_id] => 10
[description] =>
)
)
[CmArea] => Array
(
[id] => 1
[area] => manchester
[status] => 1
)
[CmType] => Array
(
[id] => 4
[type] => Designer
[status] => 1
)
)
)
)
)
[_count:protected] => 1
)
[_currentItemCount:protected] =>
[_currentItems:protected] =>
[_currentPageNumber:protected] => 1
[_filter:protected] =>
[_itemCountPerPage:protected] => 2
[_pageCount:protected] => 1
[_pageRange:protected] =>
[_pages:protected] =>
[_view:protected] =>
)
I am attempting sort this array by each stdClass Object's [title] in an Ubercart order.tpl.php file. I have tried doing just a normal sort but since they all are stdClass Objects it doesn't do anything.
<pre>Array
(
[0] => stdClass Object
(
[order_product_id] => 1157
[order_id] => 142
[nid] => 38
[title] => Eatonville Forest Camping Permit
[manufacturer] =>
[model] => eatonville-camp-permit
[qty] => 1
[cost] => 9.00000
[price] => 10.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 0
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[1] => stdClass Object
(
[order_product_id] => 1158
[order_id] => 142
[nid] => 35
[title] => Eatonville Forest Motorized Recreation Access Permit and Release of Liability
[manufacturer] =>
[model] => eatonville-motor-rec-access
[qty] => 1
[cost] => 175.00000
[price] => 175.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[2] => stdClass Object
(
[order_product_id] => 1159
[order_id] => 142
[nid] => 31
[title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Families and Release of Liability
[manufacturer] =>
[model] => snoqualmie-family-non-motor-rec-access
[qty] => 1
[cost] => 150.00000
[price] => 150.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[3] => stdClass Object
(
[order_product_id] => 1160
[order_id] => 142
[nid] => 40
[title] => Snoqualmie Forest Camping Permit
[manufacturer] =>
[model] => snoqualmie-camp-permit
[qty] => 1
[cost] => 300.00000
[price] => 300.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[4] => stdClass Object
(
[order_product_id] => 1161
[order_id] => 142
[nid] => 39
[title] => White River Forest Camping Permit
[manufacturer] =>
[model] => whiteriver-camp-permit
[qty] => 1
[cost] => 300.00000
[price] => 300.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[5] => stdClass Object
(
[order_product_id] => 1162
[order_id] => 142
[nid] => 30
[title] => White River Forest Family Non-motorized Recreation Access Permit for Families and Release of Liability
[manufacturer] =>
[model] => whiteriver-family-non-motor-rec-access
[qty] => 1
[cost] => 150.00000
[price] => 150.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[6] => stdClass Object
(
[order_product_id] => 1163
[order_id] => 142
[nid] => 33
[title] => White River Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability
[manufacturer] =>
[model] => whiteriver-non-motor-rec-access
[qty] => 1
[cost] => 75.00000
[price] => 75.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
[7] => stdClass Object
(
[order_product_id] => 1164
[order_id] => 142
[nid] => 34
[title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability
[manufacturer] =>
[model] => snoqualmie-non-motor-rec-access
[qty] => 1
[cost] => 75.00000
[price] => 75.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[restrict_qty] => 1
[module] => uc_product
)
[order_uid] => 13
)
)</pre>
This is the code controlling it in the order.tpl.php
<pre>php if (is_array($order->products)) {
$context = array(
'revision' => 'formatted',
'type' => 'order_product',
'subject' => array(
'order' => $order,
),
);
}
</pre>
I think usort() should work:
function sortByTitle($a, $b){
return strcmp($a->title, $b->title);
}
usort($theArray, 'sortByTitle');
You may want to throw in some checking to make sure the title member actually exists.
[5] => Array
(
[id] => 29372
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1469
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 21cf9d7d09d403251ba5d01ff33cd089.jpg
[coverid] => 1178
[inserted_by] => 0
[inserted_date] => 2011-02-11 13:55:23
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1469
[media_format] => VCD
[price] => 29000
[discount] => 5
)
)
[media_format] => VCD
)
[6] => Array
(
[id] => 30074
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1470
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 149ddd4d1d5e567c1300d4831323e1c5.jpg
[coverid] => 1177
[inserted_by] => 6
[inserted_date] => 2011-02-16 15:18:58
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1470
[media_format] => DVD
[price] => 39000
[discount] => 0
)
)
[media_format] => DVD
)
I want the result by media_format = DVD so the whole array is gone, is there anyway to delete an array or remove it?
The same way you "delete" any variable:
unset($array[$index]);