Koper
Koper

Reputation: 123

Converting array to an object with an array inside it

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.

Upvotes: 1

Views: 68

Answers (3)

PHPJungle
PHPJungle

Reputation: 522

Is this what you want?

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

$obj_person = (Object)$person;

Outputs:

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"
  }
}

Upvotes: 0

Jens A. Koch
Jens A. Koch

Reputation: 41737

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"
    }
  }
}

Upvotes: 1

AJReading
AJReading

Reputation: 1213

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;

Upvotes: 0

Related Questions