If next item in looping array exists

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

Output:

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

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

Answers


What about the easy one?

$_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;

You can e.g. use the CachingIterator

<?php
$source = array(1,2,3,4,5,6);
$cit = new CachingIterator(new ArrayIterator($source));

foreach($cit as $e) {
    if ( !$cit->hasNext() ) {
        echo 'last element: ';
    }
    echo $e, "\n";
}

prints

1
2
3
4
5
last element: 6

...or in this case a simple join(', ', $this->rows)


Need Your Help

What does mutex lock fail with invalid argument mean?

c++ boost

This code is called in my main process and compiles fine, but when executed always throws the error below.

How do I make a variable that has its values pre-edited at declaration?

c++ variables

I want to make a normalized coordinate system and I was wondering if there was a way to make a variable that will do this on declaration:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.