简体   繁体   中英

Removing extra commas when echoing out imploded array after $_POST

I have an array with 100 or so items in it. I'm pulling from a huge checklist in a form. The items the user checks off are displayed on the next page after submission.

I have the items displaying, but there are several commas populating the page as well. No doubt from the variables that remained unfilled.

Here are a couple of code snippets to give you the general idea:

<!-- Front Underspoiler -->
<input type="checkbox" id="frontUnderspoiler" name="frontUnderspoiler" value="Front Underspoiler"> Front Underspoiler
<br>
<br>
<!-- Bumper Corner Moldings -->
<input type="checkbox" id="bumperCornerMoldings" name="bumperCornerMoldings" value="Bumper Corner Moldings"> Bumper Corner Moldings
<br>
<br>
<!-- Bumper Corner Molding-(2 PC) -->
<input type="checkbox" id="bumperCornerMoldingTwoPc" name="bumperCornerMoldingTwoPc" value="Bumper Corner Molding-(2 PC)"> Bumper Corner Molding-(2 PC)
<br>
<br>

This is how the form is structured for each item. When the form is submitted the user is redirected to the next page. Here's how my $_POST and array are set up:

// ACCESSORIES
$allWeatherFloorMats = trim($_POST["allWeatherFloorMats"]);
$oneTenPowerOutlet = trim($_POST["oneTenPowerOutlet"]);
$frontLicPlateBrack = trim($_POST["frontLicPlateBrack"]);
$ashTray = trim($_POST["ashTray"]);
$ashTrayMountingSleeve = trim($_POST["ashTrayMountingSleeve"]);
$cigaretteLighter = trim($_POST["cigaretteLighter"]);
$sideSillPlates = trim($_POST["sideSillPlates"]);

Bringing the the data in structured into an array:

$accessoriesArray = array(
  '1' => $allWeatherFloorMats,
  '2' => $oneTenPowerOutlet,
  '3' => $frontLicPlateBrack,
  '4' => $ashTray,
  '5' => $ashTrayMountingSleeve,
  '6' => $cigaretteLighter,
  '7' => $sideSillPlates,
  '8' => $frontSideSillPlate,
  '9' => $strakeKit);

The last bit in the body of the page that echos the array with commas:

<?php echo implode(',',$accessoriesArray); ?>

Here's what I'm receiving:

Your Vehicle Includes ,,,,,,,,,,,,,,,,,,Rear Bumper Applique,,,Side Window Deflectors,,,,,,,,,,,,Bumper Bib,,,Sunshade,,Sport Grille Gunmetal Trim,,,,,,,,,,,,,Tweeter Kit,,,,,,Footwell Illumination Kit- Blue,,,Exterior Auto-Dimming for Blind Spot Detection,,,,,,,,Alloy Wheels,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,STI Carbon Fiber Trunk Trim,,,,.

Is there a way to remove the items in the array that are causing the commas to appear prior to me echoing out the result? Thanks for taking a the time to check this out. I'm relatively new to PHP. Any advice would be greatly appreciated.

<?php echo implode(',', array_filter($accessoriesArray)); ?>

When array_filter is called without a second argument, all entries of array equal to FALSE will be removed. (The empty string converts to FALSE.)

You have answers with array_filter() , however I would say that your code is needlessly complex. I would structure the checkboxes as a data array like so:

name="data[]" value="Front Underspoiler"
name="data[]" value="Bumper Corner Moldings"
etc...

Then you only get the checked ones in the data array:

$accessoriesArray = $_POST['data'];

If needed to trim then:

$accessoriesArray = array_map('trim', $_POST['data']);

You can use array_filter() and give that your array and a function that gets every single element passed and should decide whether this element belongs to the final array or not by returning either true or false .

That function can either be a name of a function as a string, or an anonymous function (and any other "callable" pointer to a function or class method).

$largeArray = [...];
$outputArray = array_filter($largeArray, function($element) {
    if (empty($element)) { return false; }
    return true;
});
echo implode(',', $outputArray);

Looks like you have some empty values. So you can do this:

implode(',', array_filter($accessarray));

This will strip off all empty values see here for further referece: http://php.net/manual/en/function.array-filter.php

array_filter - excelent idea. But this is bad code:

$accessoriesArray = array(
  '1' => $allWeatherFloorMats,
  '2' => $oneTenPowerOutlet,
  '3' => $frontLicPlateBrack,
  '4' => $ashTray,
  '5' => $ashTrayMountingSleeve,
  '6' => $cigaretteLighter,
  '7' => $sideSillPlates,
  '8' => $frontSideSillPlate,
  '9' => $strakeKit,);

you have array, and you have this array. You need filter $_POST and ignore empty.

$result = [];
$keys = [
    'allWeatherFloorMats',
    'oneTenPowerOutlet',
    'frontLicPlateBrack',
    'ashTray',
    'ashTrayMountingSleeve',
    'cigaretteLighter',
    'sideSillPlates',
];
foreach($keys as $key)
{
    if(isset($_POST[$key]))
    {
        $_POST[$key] = trim($_POST[$key]);
        if(!empty($_POST[$key]))
        {
            $result[$key] = $_POST[$key];
        }
    }
}
echo implode(',', $_POST);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM