Wednesday, January 4

How to convert an array to object in PHP?

How to convert an array to object in PHP?
This post will show you how to convert convert an array to object with different conditions of array in PHP.
Convert array to object using json.


The Best, quick, dirty, very effective and easy way (also my personal recommendations) to convert array to objects using json_encode and json_decode, it will turn the entire array (also including sub elements of array) into an object.

// Convert array into an object
$object = json_decode(json_encode($array));

var_dump ($object); // print object value with var_dump   


Convert associative array in to object

$array = Array ( 'keys' => "status value" );
// convert array in to object
$object = (object) $array;

var_dump ($object); // print object value with var_dump

var_dump($object->keys); // echo object value using key

Convert mulch-dimensional array( with same associative array) to object with simple way


$array = array (
    "0" => Array( "status_key" => "test value of keys 0" ),
    "1" => Array( "status_key" => "test value of keys 1" ),
    "2" => Array( "status_key" => "test value of keys 2" )
);

simple way convert array in to object

// convert array in to object
$object = (object) $array;
var_dump ($object); // print object value with var_dump

This is another way to convert array to object with foreach loop

$object = new stdClass();

foreach ($array as $keys => $value ) {
    $object->{$keys} = $value;
}
var_dump ($object); // print object value with var_dump

In this mulch-dimensional array we will convert into an object.

$array = array (
    "test array",
    array( "keys test 1 value" ,"keys test 2 value" ,"keys test 3 value" ),
    array( "keys test 1 value" ,"keys test 2 value" ,"keys test 3 value" ),
    array( "keys test 1 value" ,"keys test 2 value" ,"keys test 3 value" ),
    array( "keys test 1 value" ,"keys test 2 value" ,"keys test 3 value" ),
);

$object = new ArrayObject($array);
var_dump ($object); // print object value with var_dump

foreach ($object as $keys => $val){
    if (is_array( $val )) {
        foreach($val as $sub_key => $sub_val) {
           echo $sub_key . ' => ' . $sub_val."<br>";
        }
    }
    else
    {
           echo $val."<br>";
    }
}

Convert array to object using function (with same associative array)

function arrat_to_object($array) {
    $objects = new stdClass();
    foreach ($array as $keys => $value) {
        if (is_array($value)) {
            $value = arrat_to_object($value);
        }
        $objects->$keys = $value;
    }
    return $objects;
}


$object = arrat_to_object($array); // pass array
var_dump ($object); // print object value with var_dump

Convert Manually array in to an object

$object = object; // assign array
foreach ($array as $keys => $values) {
    $object->{$keys} = $values;
}
var_dump ($object); // print object value with var_dump 


 

No comments:

Post a Comment

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...