0

I have two stdclass objects namely

stdClass Object
(
    [apple] => 10
    [orange] => 10
    [grape] => 10
    [banana] => 20
    [mango] => 30
)

stdClass Object
(
    [apple] => 15
)

Can I update apple like a normal array operation? Or is there another way to do so?

Also whats the difference between this and a normal array?

2 Answers2

0

You would loop through them using a normal foreach loop:

$obj = new StdClass;

$obj->test = 1;
$obj->more_testing = 2;

foreach ($obj as $key => $value) {
    echo $key . ': ' . $value . '\n\n';
}

or you could cast them as an your objects as an array using (array).

Live Example

Repl

Reading Material

Iterations

Script47
  • 14,230
  • 4
  • 45
  • 66
0

See the official documentation on comparing objects:

When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with ==), and are instances of the same class.

When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.

Also, see here: What's the fastest way to compare two objects in PHP?

Community
  • 1
  • 1
Bananaapple
  • 2,984
  • 2
  • 25
  • 38