Answers for "convert class to array php"

PHP
1

convert object to array php

$array = (array) $object;
Posted by: Guest on June-24-2021
1

convert object to array in php

// It will work Perfectly Fine.
$arr = json_decode(json_encode($obj), true);
Posted by: Guest on August-31-2021
3

convert object to array php

<?php 
class sample { 
      
    /* Member variables */
    var $var1; 
    var $var2; 
      
    function __construct( $par1, $par2 )  
    { 
        $this->var1 = $par1; 
        $this->var2 = $par2; 
    } 
} 
  
// Creating the object 
$myObj = new sample(1000, "second"); 
echo "Before conversion: \n"; 
var_dump($myObj); 
  
// Converting object to associative array 
$myArray = json_decode(json_encode($myObj), true); 
echo "After conversion: \n"; 
var_dump($myArray); 
?> 
  
Output:
Before conversion: 
object(sample)#1 (2) {
  ["var1"]=>
  int(1000)
  ["var2"]=>
  string(6) "second"
}

After conversion: 
array(2) {
  ["var1"]=>
  int(1000)
  ["var2"]=>
  string(6) "second"
}
Posted by: Guest on April-04-2020

Code answers related to "convert class to array php"

Browse Popular Code Answers by Language