Home > Backend Development > PHP Tutorial > How to Quickly Convert a PHP Object into an Associative Array?

How to Quickly Convert a PHP Object into an Associative Array?

DDD
Release: 2024-12-21 19:07:10
Original
474 people have browsed it

How to Quickly Convert a PHP Object into an Associative Array?

How to Quickly Convert a PHP Object to an Associative Array

When integrating APIs that operate with object-based data, the need often arises to convert these objects to arrays to align with existing code using arrays. Here's a concise and straightforward solution to perform this conversion:

Typecast the Object:

To convert an object to an associative array, you can simply typecast it like so:

$array = (array) $yourObject;
Copy after login

This typecasting method is a direct and efficient approach. However, it's important to note that it only performs a shallow conversion.

Property Accessibility in Cast Arrays:

When typecasting an object to an array, various rules apply regarding property accessibility:

  • Public properties are directly accessible in the array.
  • Private properties have 'foo' prepended to the variable name.
  • Protected properties have '*foo' prepended to the variable name.

Integer Property Conversion:

Integer properties are inaccessible and will not appear in the converted array.

Example: Converting a Simple Object:

$object = new StdClass;
$object->foo = 1;
$object->bar = 2;

var_dump( (array) $object );
Copy after login

Output:

array(2) {
  'foo' => int(1)
  'bar' => int(2)
}
Copy after login

Example: Converting a Complex Object with Private and Protected Properties:

class Foo {
    private $foo;
    protected $bar;
    public $baz;

    public function __construct() {
        $this->foo = 1;
        $this->bar = 2;
        $this->baz = new StdClass;
    }
}

var_dump( (array) new Foo );
Copy after login

Output:

array(3) {
  'Foofoo' => int(1)
  '*bar' => int(2)
  'baz' => class stdClass#2 (0) {}
}
Copy after login

As you can observe, private and protected properties are present in the converted array with the specified prepended prefixes.

Deep Casting and Non-Public Properties:

Note that this typecasting method does not perform deep casting. To access non-public attributes, you may need to explicitly apply the null bytes, as explained in the official PHP documentation.

For more in-depth information, refer to the linked resources:

  • PHP Built-in Class stdClass: https://www.php.net/manual/en/class.stdclass.php
  • Fast PHP Object to Array Conversion Blog Post: https://dereuromark.de/2011/02/03/fast-php-object-to-array-conversion/

The above is the detailed content of How to Quickly Convert a PHP Object into an Associative Array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template