0

私は最近フォームで多くの作業を行っており、自分自身が繰り返し見ているいくつかの側面を単純化するために php スクリプトを作成することにしました。可能であれば、次のコード:

function add($name,$input,&$array)
{
 $p = explode('[',$name);
 if(isset($p[1]))
 {
  list($key) = explode(']',$p[1]);
  if(ctype_digit($key))
  {
    $array['forms'][$p[0]][$key] = $input;
  }else{
    $array['forms'][$p[0]][] = $input;
  } 
 }else{
    $array['forms'][$name] = $input;
 }
}


$array = array();
add('image[]','',$array);
add('image[8]','',$array);
add('image[1]','',$array);
add('image[]','',$array);

echo '<PLAINTEXT>';
print_r($array);

$array を次のようにします。

Array
(
    [forms] => Array
        (
            [image] => Array
                (
                    [0] => 
                    [8] => 
                    [1] => 
                    [9] => 
                )

        )

)

ここでの問題は、$name として「画像」を追加する場合、Posted されたかのように配列に追加する必要があるため、image[] を入力すると配列 (image=>data) になることです。それはarray(image=>array(0=>data))になります。

コードが大きすぎることがわかりました。「image[]」を解析するparse_strを見つけましたが、名前を個別に追加する必要があるため、役に立ちませんでした...

この関数をよりエレガントにすることはできますか?

説明:

配列に追加する名前のリストの一部であるかのように、「name[]」を配列に追加するより良い方法はありますか。

そのため、$array を上書きしない parse_str の置換が必要です。例:

$array = array();
parse_str('image[]=test',$array);
parse_str('image[]=test1',$array);
parse_str('image[]=test2',$array);

しかし、結果は次のようになります。

Array
(
    [image] => Array
        (
            [0] => test2
        )

)

しかし、次のようにする必要があります:

Array
(
    [image] => Array
        (
            [0] => test
            [1] => test1
            [2] => test2
        )

)

これにより、上記の関数が本当に単純化されます。

4

3 に答える 3

1

なぜpregがまだ言及されていないのか、私にはよくわかりませんが、それはあなたが本当に望んでいることのようです

function add( $input, &$target )
{
    // sanity check.  If the brackets are missing, add them.
    if( strpos( $input, '[' ) === FALSE )
    {
        $input = str_replace( '=', '[]=',  $input );
    }
    // The regex means, "Get the starting variable name segment" 
    // (begins with a letter here, you could just make it \w+)
    // followed by the portion between the left and right brackets
    // and finally by the value after the period until the end of the input
    preg_match( '/^([a-zA-Z]\w*)\[(\w*)\]=?(.*)$/', $input, $matches );

    // the first value in the matches array is the original string.
    array_shift( $matches );

    // sanity check -- if the string doesn't match the above regex, ignore it.
    if( !isset( $matches[ 1 ] ) ) return;
    $m1 = $matches[ 1 ];
    $m2 = ( isset( $matches[ 2 ] ) )? $matches[ 2 ]: NULL;

    // numeric keys are translated to the equivalent of array_push.
    if( is_numeric( $m1 ) || $m1 == "" )
    {
        $target[ $matches[ 0 ] ][] = $m2;
    }
    // non-numerics are kept as is.
    else
    {
        $target[ $matches[ 0 ] ][ $m1 ] = $m2;
    }
}
于 2010-10-22T10:02:15.403 に答える
1

おそらく、array_merge_recursiveをスローすると、コードを少し単純化するのに役立ちます。Johnのメソッドシグネチャから作業すると、次のようになります。

function add($value, &$target) {
    $temp = array();
    parse_str($value, $temp);
    $target = array_merge_recursive($target, $temp);
}

$array = array();
add('image[]=test',$array);
add('image[text8]=test4',$array);
add('image[]=test2',$array);
add('video[test5]',$array);

(予想通り)これも

Array
(
    [image] => Array
        (
            [0] => test
            [text8] => test4
            [1] => test2
        )

    [video] => Array
        (
            [test5] => 
        )

)

うまくいけば、これは役に立ちます:)

編集 :

まったく同じ動作(最小限の行数)が必要な場合は、いつでもクエリ文字列を再構築し、値を追加して再度解析することができます。結果のコードは最適でもきれいでもありませんが、それは仕事をします;)。図解:

function add($value, &$target) {
    $query = urldecode(http_build_query($target, '', '&'));
    $query = "{$query}&{$value}";
    parse_str($query, $target);
}

$array = array();

add($array, 'image[]');
add($array, 'image[8]=test4');
add($array, 'image[1]=test2');
add($array, 'image[]');

print_r($array);

結果として

Array
(
    [image] => Array
        (
            [0] => 
            [8] => test4
            [1] => test2
            [9] => 
        )

)
于 2010-10-22T08:41:08.110 に答える
1

そうです、あなたの明確化を念頭に置いた別の試み:

function add($name,&$array)
{
    $type = explode('[',$name);
    $key = str_replace(array($type['0'], ']=', '[', ']'), '', $name);
    $array[$type['0']][] = $key;
}

$array = array();
add('image[]=test',$array);
add('image[test1]',$array);
add('image[]=test2',$array);
add('video[test5]',$array);

echo '<PLAINTEXT>';
print_r($array);

戻ります:

Array
(
  [image] => Array
      (
          [0] => test
          [1] => test1
          [2] => test2
      )

  [video] => Array
      (
          [0] => test5
      )

)

OK、最後にもう一度!私の知る限り適切な関数はなく、既存のコードをこれ以上整理するのはそれほど簡単ではありませんが、最善を尽くしました!

function add($name,&$array)
{
    $type = explode('[',$name);
    $key = (!empty($type[1])) ? explode(']', $type[1]) : false;
    $value = str_replace(array($key[0], $type[0], ']=', '[', ']'), '', $name);
    if ($key[0]) $array[$type['0']][$key[0]] = $value;
    else $array[$type['0']][] = $value;
}

$array = array();
add('image[]=test',$array);
add('image[text8]=test4',$array);
add('image[]=test2',$array);
add('video[test5]',$array);

echo '<PLAINTEXT>';
print_r($array);

戻ります:

Array
(
  [image] => Array
      (
          [0] => test
          [text8] => test4
          [1] => test2
      )

  [video] => Array
      (
          [test5] => 
      )

)
于 2010-10-21T12:19:51.733 に答える