views:

306

answers:

6

How can i convert an array like this to 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
        )

)
A: 

The easy way would be

$object = (object)$array;

But that's not what you want. If you want objects you want to achieve something, but that#s missing in this question. Using objects just for the reason of using objects makes no sense.

johannes
doesnt work, i did that before i asked the question here so there must be another way to doit
streetparade
Why does he have to give his reason for wanting to use objects? I don't think that's relevant to *how* it's done. Maybe he needs to json_encode them, or serialize them? There could be dozens of reasons to do this.
zombat
hmm.. i looked at the browser output it looks like this object(stdClass)#150 (130) { [0]=> array(1) { ["status"]=> string(130) "At long last Mac and Linux users don't have to feel like second class citizens in Chrome land: they've got official beta versio…" }officialy that is an object but how to iterate throw this that i can acces status like $obj->status any idea?
streetparade
zombat, JSON encode is no reason for using an object, there is a flag to json_encode() to use objects. with serialize one would need a specific object type expected by the receiver.And in general I try to help with the _actual_ problem. for me this question implies that there is an architectural mistake somewhere else.
johannes
+7  A: 

You could create an standard class variable (very basic object):

$object = new stdClass();

and loop through your array and re-assign the values

foreach ($array as $key => $value)
{
    $object->$key = $value;
}

Keep in mind that this won't work for arrays with numeric keys/indices as variables cannot start with numbers.

Janek
"as variables cannot start with numbers", yes they can: $object->{3} = 'xyz';
chelmertz
+3  A: 

There's no built-in method to do it as far as I'm aware, but it's as easy as a simple loop:

    $obj= new stdClass();

    foreach ($array as $k=> $v) {
        $obj->{$k} = $v;
    }

You can expound on that if you need it to build your object recursively.

zombat
A: 

CakePHP has a recursive Set::map class that basically maps an array into an object. You may need to change what the array looks like in order to make the object look the way you want it.

http://api.cakephp.org/view_source/set/#line-158

Worst case, you may be able to get a few ideas from this function.

Dooltaz
+1  A: 

3 ways:

1) Fake a real object

    class convert{

    public $varible;

    public function __construct($array)
    {
     $this = $array;
    }

    public static function toObject($array)
    {
     $array = new convert($array);
     return $array;
    }

}


2) Convert the $array into an object by casting it to an object

// ...
$array =& (object) $array;  // reference operator, may solve your not working casting
// ...


3) Manually convert the $array into an object

$object = object;
foreach($arr as $key => $value)
{
$object->{$key} = $value;
}
daemonfire300
hmm thanks but your face class gives the following error Fatal error: Cannot re-assign $this in /var/www/bot/inc/twitter-bot.php on line 10
streetparade
and typcasint @ reference isnt a good idea even it wouldnt work here is what i got unexpected T_OBJECT_CAST, expecting T_NEW or T_STRING or T_VARIABLE or '$'
streetparade
I am sorry, yes it was obvious not tested yet, but I thought "give a quick and brief answer", next time I ll spent more time in testing ;)I ll go on with further testings about casting when I upgraded to PHP v5.3.1.
daemonfire300
+1  A: 

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;
}
streetparade