?
Solved

how to modify or read stdClass object inside array?

Posted on 2014-12-07
13
Medium Priority
?
197 Views
Last Modified: 2014-12-07
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?
0
Comment
Question by:aboo_s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
13 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40485478
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

0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485507
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
$usable_rates
the first one starts with [3]
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485514
Tried this:
$usable_rates->$shipping_price=15;

Open in new window

and got:
Cannot use object of type stdClass as array in ..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:aboo_s
ID: 40485524
I used this:
$usable_rates->$shipping_price=15;

Open in new window

got no errors ,but value printed of shipping_price was blank!
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485527
This:
$usable_rates->'3'->$shipping_price=15;
is a no go as well!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40485528
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.
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485529
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..
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40485531
Yes, but the reason to prefer var_dump() is that it also gives you data types as well as data contents!
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485533
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

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40485570
Please see the note here:
http://php.net/manual/en/language.oop5.properties.php#108320

$array[1]->shipping_price = 10;  WORKS
$array[1]->$shipping_price = 10;  IS AN ERROR

Here you go: http://iconoun.com/demo//temp_aboo_s.php

<?php // demo/temp_aboo_s.php
error_reporting(E_ALL);

/**
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28575851.html#a40485533
 *
 * 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)
 *      // ETC ETC
 */

// MAKE THINGS EASIER TO READ
echo '<pre>';

// CREATE A TEST OBJECT AND INJECT PROPERTIES
$obj = new StdClass;
$obj->shipping_id           = "3";
$obj->shipping_type         = "manual";
$obj->shipping_zone_namekey = "";
$obj->shipping_tax_id       = "0";
$obj->shipping_price        = (float) 40;

// ASSIGN THE OBJECT TO A POSITION OF AN ARRAY
$arr[3] = $obj;

// DOES IT LOOK RIGHT? (YES, CLOSE ENOUGH)
var_dump($arr);

// SHOW HOW TO GET A PROPERTY FROM THE OBJECT
$dat = $arr[3]->shipping_type;
var_dump($dat);

// SHOW HOW TO SET A PROPERTY IN THE OBJECT
$arr[3]->shipping_price = 40.01;

// SHOW THE ARRAY CONTAINING THE CHANGED OBJECT
var_dump($arr);

Open in new window

HTH, ~Ray
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485611
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:
$usable_rates->shipping_price=15.0;
		die( 
		"***".$usable_rates->shipping_price."***");

Open in new window

0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485615
output was this:
******
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40485619
Alright .. Got it
This worked:
$usable_rates[0]->shipping_price=15;

Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question