-1

In this example I have two possibilities to display, which car is being hired, where and what fuel type it has. If the car is chosen for different weekday, it comes this:

Ford, Philadelphia, Diesel

Toyota, Philadelphia, Diesel

Nissan, Philadelphia, Diesel, ,

If I choose One car for the whole week it comes only the one car:

Ford, Philadelphia, Diesel

Here is the code, I am using to display them:

    } else {
    $carnames = array();
    foreach ($carname as $carraw) {
        $hirestation = array();
        if (!empty($carraw->name)) {
            $hirestation[] = $carraw->name;
        }
        if (!empty($carraw->cartype)) {
            $hirestation[] = $carraw->cartype;
        }
        if (!empty($carraw->address)) {
            $hirestation[] = $carraw->carfuel;
        }
        $carname = implode(', ', $hirestation);

        $dedupedroomnames[] = $roomname;
    }
    $carnames = implode('; ', $carnames);
    $strcar = (!empty($allcarnames)) ? $carnames : $carnull;
}

The problem is, that in case there were multiple cars for the week, at the end there are always this two comas:

Ford, Philadelphia, Diesel, ,

After examing it, I found, that there comes the car for the whole week, if I choose it together with the single choices:

Ford, Philadelphia, Diesel, Dodge, D.C, Gas,

What I am missing within the code?

Community
  • 1
  • 1
freudsfreund
  • 143
  • 2
  • 20

1 Answers1

3

It would seem that one or more values are blank or null.

Try to use array_filter() to filter out empty values.

You can find a similar issue here: PHP array and implode with blank/null values.

More info: http://php.net/manual/en/function.array-filter.php

Community
  • 1
  • 1
GeorgeKaf
  • 599
  • 8
  • 23