RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Convert Object (With Protected Values) To Associative Array

If you need to quickly and easily convert an object to an associative array, the following is a simple solution:

1
$array = json_decode(json_encode($object), true);

In many cases - and if the above doesn't float your boat - you may simply typecast it:

1
$array =  (array) $object;

However, neither of the above solutions work with objects that contain protected and/or private values. In our case, we were trying to access protected header values from WP's wp_remote_get function. The following function will return an associative array with values accessible via traditional means.

1
<?php 
2
/*
3
 Convert Object (With Protected Values) To Associative Array
4
 http://www.beliefmedia.com/object-to-array
5
*/
6
 
7
function beliefmedia_objarray($object) {
8
    $reflectionClass = new ReflectionClass(get_class($object));
9
    $array = array();
10
    foreach ($reflectionClass->getProperties() as $property) {
11
        $property->setAccessible(true);
12
        $array[$property->getName()] = $property->getValue($object);
13
        $property->setAccessible(false);
14
    }
15
  return $array;
16
}

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment