【问题标题】:implode() an array with multiple database columns, except on last entry PHPimplode() 具有多个数据库列的数组,除了最后一个条目 PHP
【发布时间】:2016-08-10 15:41:38
【问题描述】:

我正在使用 PHP 从数据库创建 XML 文档以导入 Adob​​e 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']));

【问题讨论】:

    标签: php arrays xml implode


    【解决方案1】:

    你可以在这个查询的帮助下做到这一点

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

    【讨论】:

    • 注意join是PHP函数implode的别名。
    【解决方案2】:

    恐怕我不能轻易理解你在这里的意图。我在您的代码中看不到任何使用任何类型数组的内容,这是implode() (又名join())的先决条件

    但是,当我在循环中构建某些东西时,我已经多次使用了这个小技巧

     $result = "";
     $comma  = "";
     foreach (...) {
       .. calculate some $value ..
       $result = $result . $comma . $value;
       $comma = ",";
     }
    

    第一次循环,$comma不是逗号!这是一个空字符串。在循环的第一次迭代结束时,它变成一个逗号,供下一次使用。这是一种就地构建任意长度的“逗号分隔字符串”的简单而有效的方法。

    HTH!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2014-05-15
      • 1970-01-01
      • 2012-02-17
      • 2015-06-09
      • 2016-03-27
      相关资源
      最近更新 更多