PHP implode()删除逗号

时间:2015-12-18 08:44:47

标签: php mysql

在此示例中,我有两种显示方式,carhiredwhere以及fuel type。如果为different weekday选择了汽车,则会出现以下情况:

  

福特,费城,柴油

     

丰田,费城,柴油

     

日产,费城,柴油,

如果我整整一周选择一辆车,那么只有一辆车:

  

福特,费城,柴油

这是代码,我用来显示它们:

    } 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;
}

problem是,如果本周有multiple cars,最后总会有two comas

  

Ford,Philadelphia,Diesel,

经过检查后,我发现car whole week来自together,如果我选择single choices function assignDatesToArbitrationCase(caseID, dateType,dt){ var urlAssignDate = ConfigService.getBaseURL() + '/legalcase/legalcasedatetype' var assignDateStr = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:leg="http://schemas.arccorp.com/services/airlinesalesreporting/legalCaseDateTypeV1"> ' + '<soapenv:Header/> <soapenv:Body> <leg:assignToRequest> <leg:entity> ' + '<leg:id legalCaseId="' + caseID + '" dateTypeId="' +dateType + '"/> ' + '<leg:legalCaseDateTypeDt>' + dateFormatForServer(dt) + '</leg:legalCaseDateTypeDt>' + '</leg:entity> </leg:assignToRequest> </soapenv:Body> </soapenv:Envelope>'; return $http({ url: urlAssignDate, method: 'POST', data: assignDateStr }).then(function (results) { var jsonObject = x2js.xml_str2json(results.data); return jsonObject; }) }

  

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

我在代码中缺少什么?

1 个答案:

答案 0 :(得分:3)

似乎一个或多个值为空或空。

尝试使用array_filter()过滤掉空值。

您可以在此处找到类似的问题:PHP array and implode with blank/null values

更多信息:http://php.net/manual/en/function.array-filter.php