2010-10-21 109 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