1

My query returns an array, but i need an object with that array in it.

In example my array:

Array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => "value3"
);

So what I am trying to do is to create a \StdClass and add this array in it. Ideally i would like the end product to look like this:

object(stdClass) {"test"=>array(1) {object(stdClass)(3) {"key1" => "value1", "key2" => "value2", "key3" => "value3"}} }

I created a function arrayToObject:

public function arrayToObject(array $array)
{
    $test = new \StdClass($array);

    return $test;
}

This does not work creates an object but my array is not is in.

How can I achieve what I am trying to do....?

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
John
  • 1,595
  • 4
  • 21
  • 44
  • This question has already been asked, check [this](http://stackoverflow.com/a/1869147/5236312). – Sylver Aug 19 '15 at 09:50

3 Answers3

0

You can do this by using (that's most clean solutio, I think):

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

Another solution would be to create empty stdClass, walktrough it (using foreach) and fill it with array data.

public function arrayToObject(array $array)
{
    $test = new \StdClass($array);
    foreach ($array as $key => $value)
    {
        $test->$key = $value;
    }
    return $test;
}
Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
0

You can also use foreach and CAST it to object

$Object = [];

foreach($array as $key => $val){
  $Object[$key] = (object)$val;
}
print_r($Object);
aldrin27
  • 3,407
  • 3
  • 29
  • 43
0

\stdClass doesn't have a constructor that accepts an array as an argument. You can add the array explicitly as a property. Try:

function arrayToObject(array $array, $property = 'myArrayProperty')
{
    $test = new \StdClass($array);
    $test->{$property} = $array;

    return $test;
}

$arr = ['foo' => 1, 'bar' => 2];
$obj = arrayToObject($arr);

// `echo $obj->myArrayProperty['foo']` => 1

print_r($obj)

This yields:

stdClass Object
(
    [myArrayProperty] => Array
        (
            [foo] => 1
            [bar] => 2
        )

)

Unless you want to actually convert the array into an object, (which is more useful in my view) where the array keys become the object properties. In which case you can perform a direct cast:

$arr = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
];

$obj = (object) $arr;

print_r($obj);

Yields:

stdClass Object
(
    [key1] => value1
    [key2] => value2
    [key3] => value2
)

Access via:

echo $obj->key1; // 'value1'

Hope this helps :)

EDIT

I had a closer look at the example output you want. You want to create a \stdClass object, with a test property. This is an array containing one element: a nested \stdClass object containing properties and values from the array.

Are you looking for something like this?

$arr = [
    'key1' => 'value1', 
    'key2' => 'value2', 
    'key3' => 'value3'
];

$obj = new \stdClass();
$obj->test = [(object) $arr];

var_dump($obj);

Yields:

class stdClass#1 (1) {
  public $test =>
  array(1) {
    [0] =>
    class stdClass#2 (3) {
      public $key1 =>
      string(6) "value1"
      public $key2 =>
      string(6) "value2"
      public $key3 =>
      string(6) "value3"
    }
  }
}
Darragh Enright
  • 13,676
  • 7
  • 41
  • 48