2

問題

フラットスカラー値から入力する必要があるネストされた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ネスト配列に変換する確立された方法(ある場合)は何ですか?

繰り返しになりますが、配列にデータを入力するための名前と値のペアが何になるかを前もって知ることはできません。これは、ここで扱っている制約の1つです。

アップデート

値(または、必要に応じて、スカラー値表現によって入力される配列キー)を知ることができないという事実は、私が扱っている特定の問題空間の制約です。これは、基本的なPHP配列構文に関する質問ではありません。

4

2 に答える 2

0

注:以下の私のPHPコードはハックですが、代わりにこれを行ってください。 以前に誰かがより良い解決策を投稿していましたが、投稿はなくなりました。つまり、フォーム内の値の配列を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']の3つのキーを持つ配列にfnameなりlnameますmiddle

可能であれば、以下に記述したコードではなく、このメソッドを使用する必要があります。それはよりきれいで、より良く、より保守しやすく、それが行われるべき方法です。


これは楽しい挑戦です。PHPの面白い方法を使用して、利点を参照します。とすれば:

  • $ inputは、$person用のキーと値のペアのみを含む配列です。
  • ピリオドは常に区切り文字です
  • 配列値と非配列値の両方を持つキーに遭遇することはありません。つまり、「contact_info」と「contact_info.foo」の両方が存在することはありません。

次に、この関数が出発点になる可能性があります。

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,
  ),

繰り返しますが、これはハックです。 これを処理するには、PHPフォーム処理を使用する必要があります。

于 2010-06-24T22:56:10.427 に答える
0

名前と値のペアがどうなるかを前もって知ることはできないと言って、あなたが何を意味するのかわかりません。

あなたの例を参照すると、これは機能します:

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

事前に鍵がわからないと、この問題を解決することはできません。事前にキーがわからない場合は、ソフトウェアの設計に重大な欠陥があります。

于 2010-06-24T22:44:55.353 に答える