Converting array to an object with an array inside it

时间:2015-06-26 10:09:36

标签: php arrays

I have an array with three keys and values. I need to convert first two keys into an object and the 3rd must remain an array but with and object inside it.

My Array:

$person = array(
    'name' => 'bob',
    'surname' => 'white',
    'address' => array(
        'street' => 'green road',
        'houseNo' => '89',
        'city' => 'Liverpool'
    )
);

I want to convert this array into an object like so:

$personInformation = json_decode(json_encode($person));

Which gives me this:

object(stdClass)(3)
{
    'name' => 'bob',
    'surname' => 'white',
    'address' => object(stdClass)(3) 
    { 
         'street' => 'green road',         
         'houseNo' => '89',
         'city' => 'Liverpool'
    }
}

But what I am after is this:

object(stdClass)(3)
{
    'name' => 'bob',
    'surname' => 'white',
    'address' => array(
       object(stdClass)(3) 
       { 
         'street' => 'green road',         
         'houseNo' => '89',
         'city' => 'Liverpool'
       }
   )
}

I'm really stuck on how to get this middle part sorted.

3 个答案:

答案 0 :(得分:1)

Turn the values of the key address into an object and reassign it, like so:

<?php

$person = array(
   'name' => bob,
   'surname' => white,
   'address' => array(
        'city' => 'Liverpool',
        'street' => 'green road',
        'houseNo' => "89"
    )
);

$address_object = (object) $person['address'];
$person = (object) $person;
$person->address = array($address_object);

var_dump($person);

Result:

object(stdClass)#2 (3) {
  ["name"]=>
  string(3) "bob"
  ["surname"]=>
  string(5) "white"
  ["address"]=>
  array(1) {
    [0]=>
    object(stdClass)#1 (3) {
      ["city"]=>
      string(9) "Liverpool"
      ["street"]=>
      string(10) "green road"
      ["houseNo"]=>
      string(2) "89"
    }
  }
}

答案 1 :(得分:0)

You could convert to an object as per your example, then simply modify the object like so:

$person = array(
    'name' => 'bob',
    'surname' => 'white',
    'address' => array(
        'street' => 'green road',
        'houseNo' => '89',
        'city' => 'Liverpool'
    )
);

$personInformation = json_decode(json_encode($person));
$personInformation->address = array($personInformation->address);

var_dump($personInformation);

That will give you:

object(stdClass)[1]
  public 'name' => string 'bob' (length=3)
  public 'surname' => string 'white' (length=5)
  public 'address' => 
    array (size=1)
      0 => 
        object(stdClass)[2]
          public 'street' => string 'green road' (length=10)
          public 'houseNo' => string '89' (length=2)
          public 'city' => string 'Liverpool' (length=9)    

Note

You can also convert arrays to object using type casting. For example:

$myArray = (object)$myArray;

答案 2 :(得分:0)

这是你想要的吗?

$person = array(
    'name' => 'bob',
    'surname' => 'white',
    'address' => array(
        'street' => 'green road',
        'houseNo' => '89',
        'city' => 'Liverpool'
    )
);

$obj_person = (Object)$person;

<强>输出:

object(stdClass)#1 (3) {
  ["name"]=>
  string(3) "bob"
  ["surname"]=>
  string(5) "white"
  ["address"]=>
  array(3) {
    ["street"]=>
    string(10) "green road"
    ["houseNo"]=>
    string(2) "89"
    ["city"]=>
    string(9) "Liverpool"
  }
}