内爆()包含多个数据库列的数组,最后一个条目PHP除外


implode() an array with multiple database columns, except on last entry PHP

我使用PHP从数据库创建XML文档导入Adobe InDesign。我想在每个条目后面加一个逗号,但最后一个不行。我试过使用implode(),但我没有运气。任何帮助都将非常感激。下面是没有添加逗号的代码。我可以在结尾处加一个逗号,但最后一个条目仍然是逗号。任何关于如何攻击的建议将是非常感激的。谢谢!

function getAssocXML ($company) {
    $retVal = "";
    // Connect to the database by creating a new mysqli object
    require_once "DBconnect.php";
    $staffResult = $mysql->query("
        SELECT company,
               fName,
               lName,
               title,
               contact1,
               contact2
          FROM staff
         WHERE company = '$company'
           AND title
          LIKE '%Associate%'
           AND archive = 0
    ");
    if ($staffResult->num_rows >= 1 && $staffResult->num_rows < 4) {
        $retVal = $retVal;
        for ($i = 0; $i < $staffResult->num_rows; $i++) {
            // Move to row number $i in the result set.
            $staffResult->data_seek($i);
            // Get all the columns for the current row as an associative array -- we named it $aRow
            $staffRow = $staffResult->fetch_assoc();
            // Write a table row to the output containing information from the current database row.
            $retVal = $retVal . "<staff>";
            $retVal = $retVal . "<name>" . $staffRow['fName'] . " " . $staffRow['lName'] . "</name>";
            $retVal = $retVal . "<contact>" . staffContact($staffRow['contact1'], $staffRow['contact2']) . "</contact>";
            $retVal = $retVal . "</staff>";
        }
        $retVal = $retVal . " &#x2014; Associate&#xD;";
        $staffResult->free();
    }
    if ($staffResult->num_rows > 4) {
        $retVal = $retVal;
        $retVal = $retVal . "<staffHeader>Associates: </staffHeader>";
        for ($i = 0; $i < $staffResult->num_rows; $i++) {
            // Move to row number $i in the result set.
            $staffResult->data_seek($i);
            // Get all the columns for the current row as an associative array -- we named it $aRow
            $staffRow = $staffResult->fetch_assoc();
            // Write a table row to the output containing information from the current database row.
            $retVal = $retVal . "<staff>";
            $retVal = $retVal . "<name>" . $staffRow['fName'] . " " . $staffRow['lName'] . "</name>";
            $retVal = $retVal . "<contact>" . staffContact($staffRow['contact1'], $staffRow['contact2']) . "</contact>";
            $retVal = $retVal . "</staff>";
        }
        $retVal = $retVal . "&#xD;";
        $staffResult->free();
    }
    return $retVal;
}
print getAssocXML(addslashes($aRow['company']));

您可以在这个查询的帮助下完成

echo join(' and ', array_filter(array_merge(array(join(', ', array_slice($array, 0, -1))), array_slice($array, -1)), 'strlen'));

$last  = array_slice($array, -1);
$first = implode(', ', array_slice($array, 0, -1));
$both  = array_filter(array_merge(array($first), $last), 'strlen');
echo implode(' and ', $both);

恐怕我没有很好地理解您的意思。我在你的代码中没有看到任何使用任何类型的数组的东西,这是implode() 的先决条件。join())

然而,这里有一个小技巧,当我在循环中构建一些时,我已经使用过很多次了:
 $result = "";
 $comma  = "";
 foreach (...) {
   .. calculate some $value ..
   $result = $result . $comma . $value;
   $comma = ",";
 }

通过循环的第一个时间,$comma 不是逗号!,它是一个空字符串。,在循环的第一次迭代结束时,它变成一个逗号,以便在下一次

时使用。这是一种简单但有效的方法来构建任意长度的"逗号分隔字符串"。

HTH !