[英]PHP: populating a nested array from flat scalar values
問題
我有一個嵌套的PHP數組,需要從平面標量值填充它。 問題是我無法提前知道嵌套的PHP數組的結構,直到我收到填充平面標量值的請求。
例
// example where we populate the array using standard PHP $person['contact_info']['fname'] = 'Attilla'; $person['contact_info']['lname'] = 'Hun'; $person['contact_info']['middle'] = 'The'; $person['hobbies'][0] = 'Looting'; $person['hobbies'][1] = 'Pillaging'; // example where we populate the array from flat scalar values // (these are obtained from the user via name-value pairs) // how can I correctly populate $person from this?? print($_GET['contact_info.fname']); // 'Smokey'; print($_GET['contact_info.middle']); // 'The'; print($_GET['contact_info.lname']); // 'Bear'; // how can I correctly populate $person from this?? print($_GET['contact_info.fname']); // 'Jabba'; print($_GET['contact_info.middle']); // 'The'; print($_GET['contact_info.lname']); // 'Hutt'; // How can I use these three flat scalars // to populate the correct slots in the nested array?
題
我知道我一定不是第一個需要將平面名稱/值對轉換為嵌套的PHP數組(或任何編程語言中的嵌套數組)的人。 將這些平面標量名稱/值對轉換為適當的PHP嵌套數組的既定方法(如果有)是什么?
僅重申一下,我無法提前知道用於填充數組的名稱-值對,這是我在此處處理的一個約束。
更新
我不知道值(或者,如果願意的話,由標量值表示填充的Array鍵)的事實是我要處理的特定問題空間的約束。 這不是有關基本PHP數組語法的問題。
我不確定您是在說什么,您無法提前知道名稱/值對是什么意思。
參考您的示例,這將起作用:
<?php
$person['contact_info']['fname'] = $_GET['contact_info.fname'];
$person['contact_info']['lname'] = $_GET['contact_info.middle'];
$person['contact_info']['middle'] = $_GET['contact_info.lname']);
?>
事先不知道值是給定的-這就是用戶輸入的方式。
您必須事先知道按鍵 。 沒有這些信息,您將不知道如何將$_GET
中的值映射到$person
值。
如果您事先不知道按鍵,就無法解決此問題。 如果您不事先知道密鑰,則軟件設計中存在嚴重缺陷。
注意:我下面的PHP代碼是一個hack,請執行此操作。 有人早些時候發布了一個更好的解決方案,但現在該帖子不見了。 簡而言之,您已經可以將表單中的值數組提交給PHP:
<form ...>
<input type="text" name="contact_info[fname]">
<input type="text" name="contact_info[lname]">
<input type="text" name="contact_info[middle]">
</form>
name屬性中的方括號與您認為的可能完全一樣。 提交時, $_POST['contact_info']
將是一個具有三個鍵的數組,分別為fname
, lname
和middle
。
如果可能的話,應該使用此方法,而不要使用我在下面編寫的代碼。 它更干凈,更好,更易於維護,這是應該完成的方式。
這是一個有趣的挑戰。 我們將使用PHP的有趣方法來引用我們的優勢。 鑒於:
然后,此功能可能是您的起點。
function nifty_splitty_magicky_goodness($input) {
// Start out with an empty array.
$person = array();
foreach($input as $k => $v) {
// This turns 'a.b' into array('a', 'b')
$key_parts = explode('.', $k);
// Here's the magic. PHP references aren't to values, but to
// the variables that contain the values. This lets us point at
// array keys without a problem. Sometimes this gets in the way...
$ref = &$person;
foreach($key_parts as $part) {
// If we didn't already turn the thing we're refering to into an array, do so.
if(!is_array($ref))
$ref = array();
// If the key doesn't exist in our reference, create it as an empty array
if(!array_key_exists($part, $ref))
$ref[$part] = array();
// Reset the reference to our new array.
$ref = &$ref[$part];
}
// Now that we're pointing deep into the nested array, we can
// set the inner-most value to what it should be.
$ref = $v;
}
return $person;
}
// Some test data.
$input = array(
'a.b' => 1,
'a.c' => 2,
'a.d.e' => 3,
'f' => 4,
'g.h' => 5
);
// Run it!
var_export(nifty_splitty_magicky_goodness($input));
// Should produce:
array (
'a' =>
array (
'b' => 1,
'c' => 2,
'd' =>
array (
'e' => 3,
),
),
'f' => 4,
'g' =>
array (
'h' => 5,
),
同樣,這是一個hack。 您應該使用PHP表單處理來為您解決這一問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.