在PHP简单数组问题 [英] Simple array question in PHP

查看:96
本文介绍了在PHP简单数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道是否有一个内置的功能,要做到这一点,所以我想我之前写浪费之一的时候,我会问。比方说,我有一个数组:

Not sure if there is a built in function to do this, so I thought I would ask before I waste the time of writing one. Lets say I have an array:

Array
    (
        ['first_name'] => John
        ['last_name'] => Doe
        ['ssn1'] => 123
        ['ssn2'] => 45
        ['ssn3'] => 6789
    )

如何创建新的密钥 ['SSN'] 将结合其他三个值,导致以下内容:

How can I create new key ['ssn'] which will combine the values of the other three, to result in the following:

Array
    (
        ['first_name'] => John
        ['last_name'] => Doe
        ['ssn'] => 123456789
    )

它有分开的,因为有3像独立的输入,例如 [] - [] - [] 来保持用户从拧起来。那么,什么会是这样做的最有效的/优雅的方式?谢谢!

It comes in separated because there are 3 separate inputs like such [   ]-[  ]-[    ] to keep the user from screwing it up. So, what would be the most effective/elegant way of doing this? Thanks!

修改

看起来像大多数用相同的简单的解决方案上来。必须给它的第一个。谢谢大家!

Looks like most came up with the same simple solution. Have to give it the first. Thanks everyone!

推荐答案

假设这数据来自Web表单,您可以使用输入的命名规则,你的优势。命名要素,以名字,姓氏,SSN [1],SSN [2],SSN [3]。 $ _REQUEST将三要素自动分配到结构化阵列。

Assuming this data comes from a web form, you could use the input naming convention to your advantage. Name the elements in order first_name, last_name, ssn[1], ssn[2], ssn[3]. $_REQUEST will automatically assign the three elements to a structured array.

<input type="text" name="ssn[1]" />
<input type="text" name="ssn[2]" />
<input type="text" name="ssn[3]" />

array
  'ssn' => 
    array
      1 => string '123' (length=3)
      2 => string '456' (length=3)
      3 => string '7890' (length=4)

那么你可以简单

$_POST['ssn'] = implode('-', $_POST['ssn']);

简单干净。

这篇关于在PHP简单数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆