1

I am running into an issue trying to push an object into an Array in PHP. My Object looks like the below sample and has 2 empty array's. When i try to use the push_array function i get the error

PHP Fatal error: Uncaught Error: Cannot use object of type ResidentialListing as array in /var/www/html/PHRETS/retsphp.php:95

Not sure why as Changes is a Array.

Here is how i call it where $r is an Object but i get same error if i try to push string.

array_push($Listing['Changes'], $r);

Below is $Listing Object

object(ResidentialListing)#86 (5) {
  ["_id"]=>
  string(36) "b77130b6-aea8-4325-9f04-fb77d2cc307f"
  ["_type"]=>
  string(11) "Residential"
  ["Record"]=>
  object(Residential_obj)#110 (18) {
    ["ListingId"]=>
    string(9) "S09100062"
    ["ListingKeyNumeric"]=>
    string(7) "4461874"
    ["StandardStatus"]=>
    string(6) "Closed"
    ["PreviousStandardStatus"]=>
    string(0) ""
    ["ParcelNumber"]=>
    string(10) "6051018010"
    ["UniversalPropertyID"]=>
    string(0) ""
    ["MLSAreaMajor"]=>
    string(17) "699 - Not Defined"
    ["MajorChangeType"]=>
    string(0) ""
  }
  ["Images"]=>
  array(0) {
  }
  ["Changes"]=>
  array(0) {
  }
}
1
  • $Listing is an object use $Listing->Changes. And better, just do $Listing->Changes[] = $r; Commented Feb 10, 2020 at 19:16

1 Answer 1

1

You're incorrectly referencing $Listing->Changes as $Listing['Changes'].

It is looking for an array key on an object when you really want the value of the property.

Change

array_push($Listing['Changes'], $r);

to

array_push($Listing->Changes, $r);
Sign up to request clarification or add additional context in comments.

2 Comments

I thought ['Changes'] was the correct syntax for accessing Arrays. and ->Changes would be a object. Is this due as the Array is empty ?
No, its because the you're accessing a property of an object that is an array. If you wanted to access a value of the array changes then your syntax would be $Listing->Changes['key_here'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.