Viewed   102 times

How can I convert an array like this to an object?

[128] => Array
    (
        [status] => "Figure A.
 Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution."
    )

[129] => Array
    (
        [status] => "The other day at work, I had some spare time"
    )

 Answers

5

This one worked for me

  function array_to_obj($array, &$obj)
  {
    foreach ($array as $key => $value)
    {
      if (is_array($value))
      {
      $obj->$key = new stdClass();
      array_to_obj($value, $obj->$key);
      }
      else
      {
        $obj->$key = $value;
      }
    }
  return $obj;
  }

function arrayToObject($array)
{
 $object= new stdClass();
 return array_to_obj($array,$object);
}

usage :

$myobject = arrayToObject($array);
print_r($myobject);

returns :

    [127] => stdClass Object
        (
            [status] => Have you ever created a really great looking website design
        )

    [128] => stdClass Object
        (
            [status] => Figure A.
 Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
        )

    [129] => stdClass Object
        (
            [status] => The other day at work, I had some spare time
        )

like usual you can loop it like:

foreach($myobject as $obj)
{
  echo $obj->status;
}
Wednesday, October 12, 2022
4

You can have the class implement the ArrayAccess interface. This will allow you to treat the object like an array without casting and you get total control over how the members are used.

Monday, August 29, 2022
 
3

Why not treat it like the math problem it is?

$i = ($ar[3]<<24) + ($ar[2]<<16) + ($ar[1]<<8) + $ar[0];

Tuesday, August 2, 2022
 
3

A quick way to do this is:

$obj = json_decode(json_encode($array));

Explanation

json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)

json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)

I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.

Thursday, August 18, 2022
3

The array key contains a marker that this should be a private property of the class test.

Compare your scripts output with the following:

$array = array(
    "xpublic" => "x1", 
    # this will become a private member:
    "x00testx00xprivate" => "x2",
    # this will become a protected member:
    "x00*x00xprotected" => "x3"
);

var_dump($array);

$obj = (object) $array;

var_dump($obj);

When serialized, the same string is used to describe the private members.

Output:

array(3) {
  ["xpublic"]=>
  string(2) "x1"
  ["testxprivate"]=>
  string(2) "x2"
  ["*xprotected"]=>
  string(2) "x3"
}

object(stdClass)#1 (3) {
  ["xpublic"]=>
  string(2) "x1"
  ["xprivate":"test":private]=>
  string(2) "x2"
  ["xprotected":protected]=>
  string(2) "x3"
}

In the output of var_dump(), the null bytes are not visible.

(Update: Added protected class member)

Saturday, December 3, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :