Laravel Foreach with a multidimensional array - php

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']

Related

Comparing array objects if the have similar values in PHP

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
}
}
}

anyone to help me convert stdClass Object to an array in php

stdClass Object ( [Order] => stdClass Object
( [id] => c62f5d272ghjjgd346lmn34
[type] => [status] => placed
[keys_status] => Keys were generated )
[Products] => Array ( [0] => stdClass Object ( [name] => Mobile Security
[devices] => 1 [months] => 12 [quantity] => 1 [msrp_price] => 9 [discount] => 0.0 [end_user_price] => 9 [reseller_margin] => 0.0 [reseller_price] => 9.01[partner_margin] => 90 [partner_price] => 0.9[Keys] => Array ( [0] => stdClass Object ( [product_id] => 21 [product_name] => Mobile Security [key] => 1234) ) ) ) [Customer] => stdClass Object ( [name] => IPPS [phone] => 25667587576 [address] => Bishop willis Street [contact_person] => Mukama Gordon [city] => Kigali [zipcode] => 250 [country_id] => 202 [state_id] => [email] => mukamatest#test.com ) [Keys] => Array ( [0] => stdClass Object ( [product_id] => 21 [product_name] => Mobile Security [key] => 1234 ) ) )
This is a really hacky way of doing it, but try
$arrayversion = json_decode(json_encode($objectversion), true);
Then
print_r($arrayversion); will be an array.

How to compare Associative Array in CodeIgniter

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?

Split multidimensional array from SOAP response

I hae vthe below code
<?php
if (is_object($checkAvailability->availableHotels))
$hotelResponse[] = $checkAvailability->availableHotels;
$hotelResponse = $checkAvailability->availableHotels;
foreach ((array)$hotelResponse as $hnum => $hotel)
?>
$hotelResponse is a Multi-dimensional array
print_r($checkAvailability->availableHotels); is generating :
Array ( [0] => stdClass Object ( [processId] => H0-41925041 [hotelCode] => ITJHRV [availabilityStatus] => InstantConfirmation [totalPrice] => 1421 [totalTax] => 0 [totalSalePrice] => 1509.38 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Classic Double or Twin Room-1 queen bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 1421 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-04-20 [amount] => 203 ) [1] => stdClass Object ( [date] => 2015-04-21 [amount] => 203 ) [2] => stdClass Object ( [date] => 2015-04-22 [amount] => 203 ) [3] => stdClass Object ( [date] => 2015-04-23 [amount] => 203 ) [4] => stdClass Object ( [date] => 2015-04-24 [amount] => 203 ) [5] => stdClass Object ( [date] => 2015-04-25 [amount] => 203 ) [6] => stdClass Object ( [date] => 2015-04-26 [amount] => 203 ) ) ) ) ) [1] => stdClass Object ( [processId] => HA-51032431 [hotelCode] => ITRR5G [availabilityStatus] => InstantConfirmation [totalPrice] => 1590 [totalTax] => 0 [totalSalePrice] => 0 [currency] => EUR [boardType] => Bed & Breakfast [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Twin Room (Including Breakfast and Wi-Fi) [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 1590 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-04-20 [amount] => 197 ) [1] => stdClass Object ( [date] => 2015-04-21 [amount] => 197 ) [2] => stdClass Object ( [date] => 2015-04-22 [amount] => 239 ) [3] => stdClass Object ( [date] => 2015-04-23 [amount] => 239 ) [4] => stdClass Object ( [date] => 2015-04-24 [amount] => 239 ) [5] => stdClass Object ( [date] => 2015-04-25 [amount] => 239 ) [6] => stdClass Object ( [date] => 2015-04-26 [amount] => 240 ) ) ) ) ) )
How can i do that get every response by echoing them as below
<?php echo $hotel->hotelCode?>
<?php echo $hotel->totalPrice?>
As i understand i need to convert the 2d array into a object but from there i have no clue. Please help.
Try this:
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
$hotelCodes[] = $hotel->hotelCode ;
//echo $hotel->hotelCode ;
//echo $hotel->totalPrice ;
}
Now you have $hotelCodes array and access it like $hotelCodes[0], $hotelCode[1] and so more.

Sorting an array with stdClass Objects

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.

Categories