Making a table from an array of objects - php

I have this object:
stdClass Object
(
[daily_inventoryID] => 1
[inventory_timestamp] => 2012-06-08 14:35:42
[inventory_date] => 2012-06-08
[inventory] =>
Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)
[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
...........
Let's say, we name the objects as $inv, i would like to display the value of $inv->inventory which is an array containing objects.
How would I do that using foreach or for loop?

foreach ($inv->inventory as $inventory) {
print_r($inventory);
}

Related

Merging two objects based on key value

I have two objects from an API I am trying to merge, one is a list of available subscription packages and one is a list of the current user's active subscription's and the corresponding status.
The first object looks like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
)
)
[has_more] =>
)
This second object looks like this:
Array
(
[0] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
[1] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
I want to combine the second object with the first object if subscription->plan->product matches product->id so it would look something like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
)
)
[has_more] =>
)
I know I can perform two foreach loops on each object and turn them into arrays with the revenant data I need and combine them that way, but I would rather merge them complete as objects.
How can I merge/combine these two in PHP?
Edit: This was my attempt at merging the two, but I am lost on how to actually make it work:
foreach($array1 as $product) {
foreach($array2 as $subscription) {
if ($subscription->plan->product == $product->id){
//how do I append to the object in the right place (where the 0 is)?
$array1->data[0]->subscription = $subscription->plan;
}
}
}

Laravel Foreach with a multidimensional array

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

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 get values from SimpleXml Object

SimpleXMLElement Object (
[results] => 2
[total_results] => 2
[plans] => SimpleXMLElement Object ( [plan] => Array ( [0] => SimpleXMLElement Object ( [id] => 1014 [type] => paid [name] => Silver [amount] => 1000.00 [interval] => 30 [notification_url] => http://www.google.com [free_trial] => 0 [occurrences] => 1 [num_customers] => 0 [status] => active ) [1] => SimpleXMLElement Object ( [id] => 1013 [type] => paid [name] => Gold [amount] => 500.00 [interval] => 30 [notification_url] => http://www.movebyu.com/ExpressEngine/index.php/?ACT=21 [free_trial] => 0 [occurrences] => 1 [num_customers] => 1 [status] => active ) ) )
),
How to get values of plans, amount from these, can any one help me?
Via XPath
http://php.net/manual/en/simplexmlelement.xpath.php

How would I read this array ("stdClass Object")

I am using the Quizlet API 2.0, and I am pretty new to this
How do I read a value(s) from something like this:
stdClass Object ( [id] => 102269 [name] => Learn Spanish with Cats! [set_count] => 3 [user_count] => 10 [created_date] => 1308035691 [is_public] => 1 [has_password] => [has_access] => 1 [has_discussion] => 1 [member_add_sets] => 1 [description] => This set is exclusively for Spanish flashcard sets with relevant cat images as the set definitions. [sets] => Array ( [0] => stdClass Object ( [id] => 6081999 [url] => http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/ [title] => Lesson 4 (with cats+dogs) [created_by] => wsvincent [term_count] => 33 [created_date] => 1311984796 [modified_date] => 1312490710 [has_images] => 1 [subjects] => Array ( [0] => spanish cats dogs ) [visibility] => public [editable] => groups [has_access] => 1 ) [1] => stdClass Object ( [id] => 5855751 [url] => http://quizlet.com/5855751/paso-a-paso-book-1-chapter-4-flash-cards/ [title] => Paso a Paso Book 1 Chapter 4 [created_by] => catdawg426 [term_count] => 30 [created_date] => 1307761267 [modified_date] => 1307819129 [has_images] => 1 [subjects] => Array ( [0] => spanish ) [visibility] => public [editable] => only_me [has_access] => 1 ) [2] => stdClass Object ( [id] => 5873819 [url] => http://quizlet.com/5873819/los-gatos-de-viaje-flash-cards/ [title] => Los Gatos de Viaje! [created_by] => tiffreja [term_count] => 21 [created_date] => 1307996657 [modified_date] => 1307996796 [has_images] => 1 [subjects] => Array ( [0] => spanish [1] => language [2] => foreign ) [visibility] => public [editable] => only_me [has_access] => 1 ) ) [members] => Array ( [0] => stdClass Object ( [username] => philfreo [role] => creator [email_notification] => 1 ) [1] => stdClass Object ( [username] => milkncookies [role] => member [email_notification] => 1 ) [2] => stdClass Object ( [username] => Icypaw [role] => member [email_notification] => ) [3] => stdClass Object ( [username] => luckycat10 [role] => member [email_notification] => ) [4] => stdClass Object ( [username] => jeffchan [role] => member [email_notification] => ) [5] => stdClass Object ( [username] => catchdave [role] => member [email_notification] => 1 ) [6] => stdClass Object ( [username] => tiffreja [role] => member [email_notification] => 1 ) [7] => stdClass Object ( [username] => catdawg426 [role] => member [email_notification] => 1 ) [8] => stdClass Object ( [username] => ihaque [role] => member [email_notification] => 1 ) [9] => stdClass Object ( [username] => jalenack [role] => member [email_notification] => 1 ) ) )
For instance, if I want to get the name of that first set, "Learn Spanish with Cats", how do I echo it via variable?
It already converts the JSON to an array I think:
$data = json_decode($json);
Your object is not an array, but rather, well, an Object. So use the -> operator to access its properties:
echo $data->name;
It contains a property which itself is an array of additional objects. For example, to get the URL of id 6081999, you would do:
echo $data->sets[0]->url;
// http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/
Here is a simple solution to convert a stdClass Object in array in php with get_object_vars
Look at : https://www.php.net/manual/en/function.get-object-vars.php
Example :
dump($array);
$var = get_object_vars($array);
dump($var);
Or replace dump() function by print_r()
Use function key
eg echo key($array)
I have looked something before, when you use the json_decode()
$data = json_decode();
U can send some parameters, the first of them is "$json", it will be the json string
{"1":"first","2":"second"}
But that json decode with one parameter return an Object and the default value of the second parameter is "false". If you want that back in array you only need to use the second parameter and send "true".
$data =json_decode($json,true);
And you can recibe it like an array. :)
In case you got stdClass Object in the array, for example
$user = $result1->fetch_object() then set $user into a variable
$val = $user->user_id (make sure user_id is your database column name, its the column name). You will get a single value in $val that comes from database.

Categories