2010-10-21 114 views
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 
(
    [forms] => Array 
     (
      [image] => Array 
       (
        [0] => 
        [8] => 
        [1] => 
        [9] => 
       ) 

     ) 

) 

這裏的問題是,如果你添加一個「圖像」作爲$名稱,那麼它必須被添加到數組中,就像它是張貼的,所以它w如果你輸入image [],那麼它將是數組(image => array(0 => data))。

我找到我的代碼是太笨重,我發現parse_str,這解析「形象[]」,但我需要的名字被單獨添加它並沒有爲我...

燦這個功能會變得更優雅嗎?

澄清:

有沒有更好的方式來增加「名稱[]」到一個數組,如果它是名稱的列表的一部分將被添加到陣列中。

,所以我需要一個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 
     ) 

) 

這將真正簡化上述功能!

+0

爲什麼不能只使用'$ array ['forms'] ['images'] [1] ='';'來訪問元素? – Hannes 2010-10-21 11:57:52

+0

爲什麼要使PHP數組呢?爲什麼不把它傳遞給HTML呢? – 2010-10-21 12:02:22

+0

雖然您將在POST數組中擁有相同的圖片(0-8-1-9)。我希望這是你想要從這個不尋常的命名 – 2010-10-21 12:04:08

回答

1

在array_merge_recursive也許扔可以幫助您簡化您的代碼位。從約翰的方法簽名工作,可能是這個樣子:

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] => 
     ) 

) 
+0

它並不像表達完全一樣,因爲它忘記了密鑰,如果它們是數字,但我可以在這裏找到用處;) – 2010-10-22 08:56:34

+0

很高興你喜歡它:),我還添加了一個基於http_build_query的替代方案,它應該導致行爲在你的文章中描述。 – hakvroot 2010-10-22 09:30:00

+0

在第二個例子中,添加變量是相反的,它給我的圖像而不是圖像,其固定的http_build_query($ target,'','&') – 2010-10-22 10:08:39

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] => 
    ) 

) 
+0

感謝您的第二次嘗試:)但你的例子應該返回數組('圖像'=> array(0 =>'test','test1'=>'',1 =>'test2'),'video'=> array('test5'=>'')) – 2010-10-21 16:31:43

+0

是不是有一些建立在這樣做的功能? – 2010-10-21 19:55:48

+0

哈哈,最後一次嘗試,我很驚訝你的耐心。但!我在你的代碼中發現了一個錯誤,如果我設置了image [0],那麼特定的0將被忽略,並將自動遞增到一個更高的值!但它比我的「更短」) – 2010-10-22 09:09:56

1

我並不十分清楚爲什麼預浸尚未提及,但是,似乎是這將是你真正想要的

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; 
    } 
} 
+0

不能與「file = test」一起使用 – 2010-10-22 10:11:55

+0

不,它看起來像name []或name [val]是givens。給我一下,我會看看我能做些什麼 – cwallenpoole 2010-10-22 10:16:59

+0

好吧,不是理想。我只是添加了一個完整性檢查。 – cwallenpoole 2010-10-22 10:24:16