如果循环数组中存在下一项 [英] If next item in looping array exists

查看:66
本文介绍了如果循环数组中存在下一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一下是否在数组循环的当前位置内还有1个或多个元素(不移动元素并计数):

I want to check to see if within the current position of an array loop there is a further 1 or more elements left (without shifting the elements and counting):

public function build() {
    $_string = 'CREATE TABLE IF NOT EXISTS `' . dbbuilder::$prefix . $this->name . '` (';
    foreach( $this->rows as $key => $row ) {
        if( $__string = $row->get_string() ) {
            $_string .= $__string . ( next( $this->rows[$key] ) ? ', ' : '' );
        }
    }
    $_string .= ') ENGINE=InnoDB  DEFAULT CHARSET=utf8;';
    $this->string = $_string;
}

输出:

[string:private] => CREATE TABLE IF NOT EXISTS `ecom_accounts` (`id` init(11) NOT NULL  AUTO_INCREMENT`name` varchar(55) NOT NULL `email_address` varchar(255) NOT NULL `password` varchar(32) NOT NULL `multisite` varchar(5) NOT NULL `roll` int(4) NOT NULL DEFAULT '0') ENGINE=InnoDB  DEFAULT CHARSET=utf8;

我以为next()可以工作,但没有用, key 也是一个字符串而不是数字.

I thought next() would work but it doesnt, also the key is a string not number.

推荐答案

简单的怎么样?

$_string = 'CREATE TABLE IF NOT EXISTS `' . dbbuilder::$prefix . $this->name . '` (';
$tmp=array();
foreach( $this->rows as $key => $row )
   if( $__string = $row->get_string() )
      $tmp[]=$__string;
$_string .= implode(',',$tmp);
$_string .= ') ENGINE=InnoDB  DEFAULT CHARSET=utf8;';
$this->string = $_string;

这篇关于如果循环数组中存在下一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆