Home  >  Article  >  Backend Development  >  How to convert object to associative array in PHP

How to convert object to associative array in PHP

藏色散人
藏色散人Original
2019-01-22 15:19:354305browse

Objects are instances of classes. It's just a sample of a class with a memory allocated. Array is a data structure that stores one or more values ​​of similar type under one name, but associative arrays are different from simple PHP arrays. An array containing string indices is called an associative array. It associates element values ​​with key values ​​rather than storing them in linear index order.

How to convert object to associative array in PHP

Method 1:

Using json_decode and json_encode methods: json_decode function accepts JSON encoded string and converts it to PHP variable, json_encode on the other hand returns the JSON encoded string of the given value.

Syntax:

$myArray = json_decode(json_encode($object), true);

Example:

<?php 
class sample { 
      
    /* 成员变量 */
    var $var1; 
    var $var2; 
      
    function __construct( $par1, $par2 )  
    { 
        $this->var1 = $par1; 
        $this->var2 = $par2; 
    } 
} 
  
//创建的对象 
$myObj = new sample(1000, "second"); 
echo "转换前: \n"; 
var_dump($myObj); 
  
// 将对象转换为关联数组
$myArray = json_decode(json_encode($myObj), true); 
echo "转换后: \n"; 
var_dump($myArray); 
?>

Output:

转换前: 
object(sample)#1 (2) {
  ["var1"]=>
  int(1000)
  ["var2"]=>
  string(6) "second"
}
转换后: 
array(2) {
  ["var1"]=>
  int(1000)
  ["var2"]=>
  string(6) "second"
}

Method 2:

Type conversion object to array: Type conversion is a method of converting a data type variable to a different data type. It is only an explicit conversion of the data type. It can convert PHP objects to arrays using the type conversion rules supported in PHP.

Syntax:

$myArray = (array) $myObj;

Example:

<?php 
class bag { 
      
    /* 成员变量 */
    var $item1; 
    var $item2; 
    var $item3; 
      
    function __construct( $par1, $par2, $par3)  
    { 
        $this->item1 = $par1; 
        $this->item2 = $par2; 
        $this->item3 = $par3; 
    } 
} 
  
// 创建myBag对象 
$myBag = new bag("Mobile", "Charger", "Cable"); 
echo "转换前: \n"; 
var_dump($myBag); 
  
// 将对象转换为数组
$myBagArray = (array)$myBag; 
echo "转换后: \n"; 
var_dump($myBagArray); 
?>

Output:

转换前: 
object(bag)#1 (3) {
  ["item1"]=>
  string(6) "Mobile"
  ["item2"]=>
  string(7) "Charger"
  ["item3"]=>
  string(5) "Cable"
}
转换后: 
array(3) {
  ["item1"]=>
  string(6) "Mobile"
  ["item2"]=>
  string(7) "Charger"
  ["item3"]=>
  string(5) "Cable"
}

This article is about the method of converting objects into associative arrays in PHP The introduction is also very simple. I hope it will be helpful to friends in need!

The above is the detailed content of How to convert object to associative array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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