2017-10-17 73 views
2

我有一個PHP對象,我試圖通過鍵獲取值而不使用foreach。通過php對象數組的鍵值獲得值

如果我做下面我能夠得到的值:

$item_data_decode->meta_data[0]->value; 

但項目可能在不同的順序,以便能夠在這種方法不計,我需要使用密鑰然而,這並不工作:

$item_data_decode->meta_data['First Name']; 

代碼:

$item_data_decode = json_decode($item_values); 
if (!empty($item_data_decode->meta_data)) { 
    $fName = $item_data_decode->meta_data['First Name']->value; 
} 

對象:

$a = new stdClass(); 
$a->meta_data = array(); 
$a->meta_data[0] = new stdClass(); 
$a->meta_data[0]->id = "2113"; 
$a->meta_data[0]->key = "First Name"; 
$a->meta_data[0]->value = "Recipient First Name"; 
$a->meta_data[1] = new stdClass(); 
$a->meta_data[1]->id = "2114"; 
$a->meta_data[1]->key = "Last Name"; 
$a->meta_data[1]->value = "Recipient Last Name"; 
$a->meta_data[2] = new stdClass(); 
$a->meta_data[2]->id = "2115"; 
$a->meta_data[2]->key = "addressLine 1"; 
$a->meta_data[2]->value = "Recipient Address Line 1"; 
$a->meta_data[3] = new stdClass(); 
$a->meta_data[3]->id = "2116"; 
$a->meta_data[3]->key = "addressLine2"; 
$a->meta_data[3]->value = "Recipient Address Line 2"; 
$a->meta_data[4] = new stdClass(); 
$a->meta_data[4]->id = "2117"; 
$a->meta_data[4]->key = "City"; 
$a->meta_data[4]->value = "Recipient Town/City"; 
$a->meta_data[5] = new stdClass(); 
$a->meta_data[5]->id = "2118"; 
$a->meta_data[5]->key = "Region"; 
$a->meta_data[5]->value = "Recipient Region/County"; 
$a->meta_data[6] = new stdClass(); 
$a->meta_data[6]->id = "2119"; 
$a->meta_data[6]->key = "Country"; 
$a->meta_data[6]->value = "N/A"; 
$a->meta_data[7] = new stdClass(); 
$a->meta_data[7]->id = "2120"; 
$a->meta_data[7]->key = "Postcode"; 
$a->meta_data[7]->value = "Recipient Postcode"; 


// outputs 
[meta_data] => Array ([0] => stdClass Object ([id] => 2113 [key] => First Name [value] => Recipient First Name) [1] => stdClass Object ([id] => 2114 [key] => Last Name [value] => Recipient Last Name) [2] => stdClass Object ([id] => 2115 [key] => addressLine 1 [value] => Recipient Address Line 1) [3] => stdClass Object ([id] => 2116 [key] => addressLine2 [value] => Recipient Address Line 2) [4] => stdClass Object ([id] => 2117 [key] => City [value] => Recipient Town/City) [5] => stdClass Object ([id] => 2118 [key] => Region [value] => Recipient Region/County) [6] => stdClass Object ([id] => 2119 [key] => Country [value] => N/A) [7] => stdClass Object ([id] => 2120 [key] => Postcode [value] => Recipient Postcode)) 

添加truejson_decode提供以下內容:

Array ([id] => 232 [order_id] => 320 [name] => Tb [product_id] => 50 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 50 [subtotal_tax] => 0 [total] => 50 [total_tax] => 0 [taxes] => Array ([total] => Array () [subtotal] => Array ()) [meta_data] => Array ()) Array ([id] => 233 [order_id] => 320 [name] => Turtle Bay Gift Card [product_id] => 50 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 30 [subtotal_tax] => 0 [total] => 30 [total_tax] => 0 [taxes] => Array ([total] => Array () [subtotal] => Array ()) [meta_data] => Array ([0] => Array ([id] => 2113 [key] => First Name [value] => Recipient First Name) [1] => Array ([id] => 2114 [key] => Last Name [value] => Recipient Last Name) [2] => Array ([id] => 2115 [key] => addressLine 1 [value] => Recipient Address Line 1) [3] => Array ([id] => 2116 [key] => addressLine2 [value] => Recipient Address Line 2) [4] => Array ([id] => 2117 [key] => City [value] => Recipient Town/City) [5] => Array ([id] => 2118 [key] => Region [value] => Recipient Region/County) [6] => Array ([id] => 2119 [key] => Country [value] => N/A) [7] => Array ([id] => 2120 [key] => Postcode [value] => Recipient Postcode))) 
+0

你的JSON是錯誤的。改變這一點,你會很好去 – JustCarty

回答

2

我個人準備這樣的數據:

$item_data_decode = json_decode($item_values, true); 
$meta_array = array_combine(array_column($item_data_decode['meta_data'], 'key'), $item_data_decode['meta_data']); 

if (!empty($meta_array['First Name'])) { 
    $fName = $meta_array['First Name']['value']; 
} 

json_decode第二PARAM確保它只返回陣列( Manual.)。這樣你就可以使用像array_columnManual)和array_combineManual)這樣的數組函數,並且得到一個非常接近你想要的結構的數組。

Test Case,因爲沒有代碼是簡寫它。

+0

我完全沒有意識到OP沒有將'true'作爲第二個參數傳遞給'json_decode()'。這一點應該完全解決問題。 –

+0

這不適合我。 '$ meta_array ['First Name']'不返回true。同樣簡單地添加'true'也不適用於原始代碼。 –

+0

@Ben Damn。我想我可以不經過測試就離開。給我幾分鐘,我更新答案。 – jh1711

0

如果你可以修改陣列結構,然後構建這樣說:

array(
    'First Name'=>array(
     'id'=>2113, 
     'value'=>'Recipient First Name' 
    ), 
    'Last Name'=>array(
     'id'=>2114, 
     'value'=>'Recipient Last Name' 
    ), 
    . . . 
); 

然後,您可以仍然使用這個數組在foreach循環中,如果需要的話,儘管有一些變化,但能夠直接訪問你想要的值。

如果您無法修改陣列結構,那麼您運氣不佳,並且如果要查找所需的值,則需要循環foreach

如果您擔心的是多次訪問數組的性能,那麼請考慮在處理之前將數組轉換爲上面的結構。

編輯

例陣列變換:

$transformed_array = array(); 
foreach($item_data_decode->meta_data as $data) { 
    $transformed_array[$data['key']] = array(
     'id'=>$data['id'], 
     'value'=>$data['value'] 
    ); 
} 
+0

不幸的是不能修改它由WooCommerce生成的數組結構。 所以唯一的辦法是對我的每個適用的密鑰做一個foreach? –

+0

正確。但是,正如我所提到的,如果您要訪問多個鍵,則應首先將該數組轉換爲關聯數組。這將允許你對'n'個不同的鍵進行'n'數組訪問操作,這將是一個'O(n^2)'算法,並將它移動到n個變換操作,然後將n個數組訪問O(n + n)= O(n)'算法的動作,使您的時間複雜度降低n倍。此外,您的代碼將會更加清潔並且更易於維護:) –

+0

謝謝@ B.Flemming您是否能夠指出我正確的方向來了解如何執行此操作的文檔? –

0

爲了保持一切不同,我在PHP腳本中包含了JSON。

訪問每個屬性是如何通常訪問對象的屬性。
請注意,由於"First name"有一個空格,因此無法用箭頭符號訪問它,並且必須用大括號括起來。對於沒有空間的任何房產,不需要花括號。

您的代碼失敗的原因是因爲您嘗試使用用於數組的方括號表示法訪問屬性。

我知道你不能編輯實際的數組輸出,但是如果你可以編輯JSON,那麼這將解決你的問題。

$item_data_decode = json_decode('{ 
    "meta_data": { 
     "First name": { 
      "id": 2113, 
      "key": "First name", 
      "value": "Recipient First Name" 
     }, 
     "Last Name": { 
      "id": 2114, 
      "key": "Last Name", 
      "value": "Recipient Last Name" 
     }, 
     "addressLine 1": { 
      "id": 2115, 
      "key": "addressLine 1", 
      "value": "Recipient Address Line 1" 
     }, 
     "addressLine2": { 
      "id": 2116, 
      "key": "addressLine2", 
      "value": "Recipient Address Line 2" 
     }, 
     "City": { 
      "id": 2117, 
      "key": "City", 
      "value": "Recipient Town/City" 
     }, 
     "Region": { 
      "id": 2118, 
      "key": "Region", 
      "value": "Recipient Region/County" 
     }, 
     "Country": { 
      "id": 2119, 
      "key": "Country", 
      "value": "N/A" 
     }, 
     "Postcode": { 
      "id": 2120, 
      "key": "Postcode", 
      "value": "Recipient Postcode" 
     } 
    } 
}'); 

var_dump($item_data_decode->meta_data->{"First name"}->value); // outputs "Recipient First Name"