将字符串添加到 PHP 数组中的值

Adding String to Value in PHP Array

我有一个 HTML 表单,它以特定任务所需的格式收集值。完成任务后,我想使用表单中的相同值执行其他任务,而无需用户再次输入信息。

我遇到的问题 运行 是第二个任务要求两个字段在发送到目的地时采用不同的格式。

这是正在发送的第二个脚本中的数组,其中左侧的键由表单中的值分配给右侧的值。

    $contactFields = array(
    // field name in myServer => field name as specified in html form
    'aaaaaaaa' =>  'email',
    'bbbbbbbb' => 'stuff',
    'cccccccc' =>  'morestuff', 
    'dddddddd' =>  'blah', 
    'eeeeeeee' =>  'blahh', 
    'ffffffff' =>  'blahh',
    'gggggggg' =>  'tobacco', //tobacco use
    'hhhhhhhh' =>  'amount', //face amount
);

我想做的是将字符串 ',000' 添加到从用户输入中获取的 'amount' 的值。同样,我不能只更改 HTML 表单上的值,因为我需要为第一个脚本设置不同格式的值。

我试过了

'hhhhhhhh' => 'amount'.',000',

没有布埃诺。我在数组之前也试过类似的方法,但是没有用。

对于接收值 'tobacco' 的字段,我试图将其从 0 或 1 值转换为是或否值。我试过这个

    if ($tobacco == 1) {
    $tobacco = "yes";
} else if ($tobacco == 0) {
    $tobacco = "no";
} else {
    $tobacco = "?";
};

但这只会导致脚本 return 一个空值。

$Tobacco 最初分配在 contactFields 数组上方

//

Assigns variables to input fields
$aaaaaaaa  = $_POST['email'];
$bbbbbbbb  = $_POST['aaaaaaaa'];
$cccccccc  = $_POST['bbbbbbbb'];
$dddddddd  = $_POST['cccccccc'];
$eeeeeeee  = $_POST['dddddddd'];
$ffffffff  = $_POST['eeeeeeee'];
$gggggggg  = $_POST['tobacco'];
$hhhhhhhh  = $_POST['amount'];

有什么建议吗?谢谢。 PHP不是我的强项

如果你想改变数组项的值,你可以这样做:

$contactFields['hhhhhhhh'] = $contactFields['hhhhhhhh'].",000";
echo $contactFields['hhhhhhhh'];
// outputs
// amount,000

$contactFields['gggggggg'] = ($contactFields['gggggggg'] == '1' ? 'yes' : 'no');
echo $contactFields['gggggggg'];
// outputs
// yes

或者如果你不想改变你的数组,你可以设置变量:

$newAmmount = $contactFields['hhhhhhhh'].",000";
echo $newAmount;
// outputs
// amount,000

$newTobacco = ($contactFields['gggggggg'] == '1' ? 'yes' : 'no');
echo $newTobacco;
// outputs
// yes

但是如前所述,如果 'amount' 确实是一个数字,请使用数字函数并且不要将其存储为字符串。