php - Converting array to an object with an array inside it -
i have array 3 keys , values. need convert first 2 keys object , 3rd must remain array , object inside it.
my array:
$person = array( 'name' => 'bob', 'surname' => 'white', 'address' => array( 'street' => 'green road', 'houseno' => '89', 'city' => 'liverpool' ) );
i want convert array object 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 after this:
object(stdclass)(3) { 'name' => 'bob', 'surname' => 'white', 'address' => array( object(stdclass)(3) { 'street' => 'green road', 'houseno' => '89', 'city' => 'liverpool' } ) }
i'm stuck on how middle part sorted.
turn values of key address
object , reassign it, 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" } } }
Comments
Post a Comment