php convert to array code example
Example 1: php object to array
$array = json_decode(json_encode($object), true);
Example 2: string to array in php
print_r(explode(',',$yourstring));
Example 3: object to array php
$array = (array) $yourObject;
Example 4: convert object to array php
<?php
class sample {
var $var1;
var $var2;
function __construct( $par1, $par2 )
{
$this->var1 = $par1;
$this->var2 = $par2;
}
}
$myObj = new sample(1000, "second");
echo "Before conversion: \n";
var_dump($myObj);
$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"
}
Example 5: object values to array php
array_values(get_object_vars($object));
Example 6: string to array php
str_split ( string $string [, int $split_length = 1 ] ) : array