-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?

2
  • try using array_filter before imploding the array - it should remove empty values Commented Dec 18, 2015 at 8:51
  • Slightly confused by your foreach loop, you are looping through $carname but within the same loop you are then imploding hirestation onto the same variable? Commented Dec 18, 2015 at 9:01

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.