1

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 Answers 3

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"
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This does not do as the OP asked. They want the address to be an array with an object as the first element of the array. Also converting the main $person array into an object.
Yes, i was too quick. Fixed it.
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;

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.