2015-06-26 77 views
1

我有一個包含三個鍵和值的數組。我需要將前兩個鍵轉換爲一個對象,並且第三個鍵必須保持爲一個數組,但其中有一個對象。將數組轉換爲其中有數組的對象

我的數組:

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

我想這個數組轉換成一個對象,像這樣:

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

這給了我這樣的:

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

但是我以後是這樣的:

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

我真的停留在如何讓這個排序中間部分。

回答

1

將鑰匙address的值轉換成一個對象,並重新分配它,就像這樣:

<?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); 

結果:

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

這不會做的任擇議定書要求。他們希望地址是一個數組,其中有一個對象作爲數組的第一個元素。還將主$ person數組轉換爲對象。 – AJReading

+0

是的,我太快了。修復。 –

0

你可以轉換爲對象,按您的例子,那麼只需修改像這樣的對象:

$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); 

這會給你:

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)  

注意

你也可以使用type casting將數組轉換爲對象。例如:

$myArray = (object)$myArray; 
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" 
    } 
}