/** * Recursively convert an array to an object. * * @param array array to convert * @return object */ public static function to_object(array $array, $class = 'stdClass') { $object = new $class(); foreach ($array as $key => $value) { if (is_array($value)) { // Convert the array to an object $value = arr::to_object($value, $class); } // Add the value to the object $object->{$key} = $value; } return $object; }
/** * Tests the arr::to_object() function. * @dataProvider to_object_provider * @group core.helpers.arr.to_object * @test */ public function to_object($input_array, $input_class, $expected_result) { $result = arr::to_object($input_array, $input_class); $this->assertEquals($expected_result, $result); }