Link to home
Avatar of Anwar Saiah
Anwar Saiah

asked on

how to modify or read stdClass object inside array?

I have this as a part of  a code generated by this print command:
$output = print_r($usable_rates, true);

Open in new window

Array ( 
[3] => stdClass Object ( 
[shipping_id] => 3 [shipping_type] => manual [shipping_zone_namekey] => [shipping_tax_id] => 0 
[shipping_price] => 40 

Open in new window

I need to modify shipping_price = 10
how do I go about it?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

It appears that there are three elements in the array, but you only showed us one of them.  Let's assume for the sake of getting a code sample that you want to modify the object in array element number 1, which is an object.

$array[1]->shipping_price = 10;

Open in new window

Avatar of Anwar Saiah
Anwar Saiah


Well unfortunately I have tried this and got:
Object of class stdClass could not be converted to string in ..

And I showed you the first slice of the code generated by the print_r command on my array
the first one starts with [3]
Tried this:

Open in new window

and got:
Cannot use object of type stdClass as array in ..
I used this:

Open in new window

got no errors ,but value printed of shipping_price was blank!
is a no go as well!
OK, let's look at the data. Please use var_dump($usable_rates) to print out the array of objects and copy/paste the output into the code snippet here.
What I gave you in the first place was a dump printed out by print_r()
any way I will also give you the var_dump
which should be just about the same..
Yes, but the reason to prefer var_dump() is that it also gives you data types as well as data contents!
array(1) { [3]=> object(stdClass)#493 (15) { ["shipping_id"]=> string(1) "3" 
["shipping_type"]=> string(6) "manual" ["shipping_zone_namekey"]=> string(0) "" 
["shipping_tax_id"]=> string(1) "0" ["shipping_price"]=> float(40)  ...

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
I tried it ,it doesn't give any error ,but it didn't give the wanted value.
It actually gave a blank value!

I did this:

Open in new window

output was this:
Alright .. Got it
This worked: