Orlando Morales-Cochran Orlando Morales-Cochran - 4 months ago 20
PHP Question

Adding String to Value in PHP Array

I have an HTML form which collects values in a format needed for a certain task. After completing the task, I would like to perform an additional task using the same values from the form without having the user enter the information in a second time.

The problem that I am running into is that the second task requires two of the fields to be formatted differently when they are sent to their destination.

Here is the array on the second script that is being sent where the keys on the left are being assigned the values on the right by the values from the form.

$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

What I am trying to do is add the string ',000' to the value of 'amount' taken from the user input. Again, I cannot just change the value on the HTML form because I need the value formatted differently for the first script.

I have tried

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

No bueno. I also tried a similar method before the array, but to no avail.

For the field receiving the value of 'tobacco', I am trying to convert it from a 0 or 1 value, to a yes or no value. I tried this

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

But that just caused the script to return a null value.

$Tobacco was originally assigned above the contactFields array as such


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'];

Any suggestions? Thanks. PHP is not my strongpoint.


If you want to change the value of the array item you can do it this way:

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

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

or you can set variables if you don't want to alter your array:

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

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

But as was mentioned, if 'amount' is truly a number, use number functions and don't store it as a string.