我正在使用一些参数编写SQL查询创建者。在Java中,只需通过数组长度检查当前数组位置,就可以很容易地从for循环中检测数组的最后一个元素。

for(int i=0; i< arr.length;i++){
     boolean isLastElem = i== (arr.length -1) ? true : false;        
}

在PHP中,它们有访问数组的非整数索引。因此必须使用foreach循环遍历数组。当您需要做出某些决定时(在我的例子中,在构建查询时附加或/和参数),这就会出现问题。

我相信一定有某种标准的方法来做这件事。

PHP中如何解决这个问题?


当前回答

从foreach数组中获取第一个和最后一个元素

foreach($array as $value) {
    if ($value === reset($array)) {
        echo 'FIRST ELEMENT!';
    }

    if ($value === end($array)) {
        echo 'LAST ITEM!';
    }
}

其他回答

用"end"怎么样? http://php.net/manual/en/function.end.php

如果您需要对除第一个或最后一个元素之外的每个元素都做一些事情,并且仅当数组中有多个元素时,我更喜欢以下解决方案。

我知道在我之前的几个月或一年,上面有很多解决方案,但我觉得这个解决方案本身就相当优雅。每个循环的检查也是一个布尔检查,而不是一个数字“i=(count-1)”检查,这可能会允许更少的开销。

循环的结构可能让人感觉很尴尬,但你可以将它与HTML表标签中的thead(开始)、tfoot(结束)、tbody(当前)的顺序进行比较。

$first = true;
foreach($array as $key => $value) {
    if ($first) {
        $first = false;
        // Do what you want to do before the first element
        echo "List of key, value pairs:\n";
    } else {
        // Do what you want to do at the end of every element
        // except the last, assuming the list has more than one element
        echo "\n";
    }
    // Do what you want to do for the current element
    echo $key . ' => ' . $value;
}

例如,在web开发术语中,如果你想在无序列表(ul)中除了最后一个元素之外的每个元素中添加border-bottom,那么你可以在除了第一个元素之外的每个元素中添加border-top (CSS:first-child,由IE7+和Firefox/Webkit支持这种逻辑,而:last-child不被IE7支持)。

您可以自由地为每个嵌套循环重用$first变量,并且事情会工作得很好,因为在第一次迭代的第一个过程中,每个循环都会使$first为false(因此中断/异常不会引起问题)。

$first = true;
foreach($array as $key => $subArray) {
    if ($first) {
        $string = "List of key => value array pairs:\n";
        $first = false;
    } else {
        echo "\n";
    }

    $string .= $key . '=>(';
    $first = true;
    foreach($subArray as $key => $value) {
        if ($first) {
            $first = false;
        } else {
            $string .= ', ';
        }
        $string .= $key . '=>' . $value;
    }
    $string .= ')';
}
echo $string;

示例输出:

List of key => value array pairs:
key1=>(v1_key1=>v1_val1, v1_key2=>v1_val2)
key2=>(v2_key1=>v2_val1, v2_key2=>v2_val2, v2_key3=>v2_val3)
key3=>(v3_key1=>v3_val1)

另一种方法是记住之前的循环结果,并将其作为最终结果:

    $result = $where = "";
    foreach ($conditions as $col => $val) {
        $result = $where .= $this->getAdapter()->quoteInto($col.' = ?', $val);
        $where .=  " AND ";
    }
    return $this->delete($result);

对于SQL查询生成脚本,或任何对第一个或最后一个元素执行不同操作的脚本,避免使用不必要的变量检查要快得多(几乎快两倍)。

目前公认的解决方案使用循环和循环内的检查,将使every_single_iteration,正确的(快速)方法如下:

$numItems = count($arr);
$i=0;
$firstitem=$arr[0];
$i++;
while($i<$numItems-1){
    $some_item=$arr[$i];
    $i++;
}
$last_item=$arr[$i];
$i++;

一个自制的基准测试显示如下:

Test1: 100000次模型morg

时间:1869.3430423737毫秒

Test2:模型运行100000次

时间:3235.6359958649毫秒

因为你寻找EOF数组的意图只是为了粘合。请了解下面的策略。你不需要EOF:

$given_array = array('column1'=>'value1',
                     'column2'=>'value2',
                     'column3'=>'value3');

$glue = '';
foreach($given_array as $column_name=>$value){
    $where .= " $glue $column_name = $value"; //appending the glue
    $glue   = 'AND';
}
echo $where;

o/p:

column1 = value1 AND column2 = value2 AND column3 = value3