Re: Re: ArrayInterator always true

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If you enable notices you will see that PHP outputs this : Notice:
ArrayIterator::next(): Array was modified outside object and internal
position is no longer valid.This line seems to be the problem //Test
if next stream is an option for ( $iterator->next();
$iterator->valid(); $iterator->next() ) { . I think that maybe the
problem is that you are accessing the array in the second for, PHP
detects that when it gets to the first for and it doesn't know if the
array is still the same and starts over . Anyway, you are trying to
iterate over the array in a pretty strange way :) . My advice is to
use $arrayobject->count() to see how many elements you have and then
iterate with a for($i=0; $i < $arrayobject->count(); $i++) . Also, if
you accept only some options than just use getopt function.

On Sat, Jan 28, 2012 at 12:10 AM, TCP <tgckpg@xxxxxxxxx> wrote:
> I've got a temporary fix but still the iterator always return true,
> still hope someone could help me out to point out the problem:)
>
> //parseOptions utilitiese by tgckpg
> function parseOptions ( $argStream, $handler ) {
>        //Chop first useless argument -- argv[0]
>        array_shift ( $argStream ) ;
>        //Initiate ArrayObject for iterator
>        $arrayobject = new ArrayObject ( $argStream ) ;
>        //Initiate iterator for iteration
>        $iterator = $arrayobject->getIterator();
>
>        //If options is set first
>        if( $iterator->valid() && preg_match ( '/^-\w$/', $iterator->current() ) ) {
>                //iterate through whole argument stream
>                for (   ; $iterator->valid(); $iterator->next() ) {
>                        //Check if reached next option
>                        if( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
>                                //Get current options
>                                $currOpt = $opts;
>                                //echo "$currOpt\n";
>                                //Test if next stream is an option
>                                for ( $iterator->next(); $iterator->valid(); $iterator->next() ) {
>                                        if ( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
>                                                //echo "$currOpt $opts\n";
>                                                $handler($currOpt);
>                                                $currOpt = $opts;
>                                        } else break;
>                                        //var_dump($iterator->valid());
>                                }
>                        }//End if
>                        //echo "$currOpt $opts\n";
>                        $handler($currOpt, $opts);
>                        //A temporary fix for infinite loop<------------------------------
>                        if(!$iterator->valid())
>                                break;
>                }// End for
>        //If option is not set first.
>        } else {
>                //Try other approach.
>        }// End if
> }
>
> On Tue, Jan 24, 2012 at 4:18 AM, TCP <tgckpg@xxxxxxxxx> wrote:
>> I'm trying to parse an $agrv array that contain options (without
>> square brackets): [-a "abc" -b "bbc" "bcc" -d "dbc" -e -f]
>> I use ArrayIterator to iterate through the line:
>>  - whenever it reach /-\w/, it read through the following qoutes
>> until it reach another /-\w/.
>>
>>
>>
>> The problem is it seems the $iterator->valid() always return TRUE and
>> cause infinte loop.
>>
>>
>> function parseOptions ( $argStream, $handler ) {
>>        //Chop first useless argument -- argv[0]
>>        array_shift ( $argStream ) ;
>>        //Initiate ArrayObject for iterator
>>        $arrayobject = new ArrayObject ( $argStream ) ;
>>        //Initiate iterator for iteration
>>        $iterator = $arrayobject->getIterator();
>>
>>        //If options is set first
>>        if( $iterator->valid() && preg_match ( '/^-\w$/', $iterator->current() ) ) {
>>                //iterate through whole argument stream
>>                for (   ; $iterator->valid(); $iterator->next() ) {
>>                        //Check if reached next option
>>                        if( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
>>                                //Get current options
>>                                $currOpt = $opts;
>>                                //echo "$currOpt\n";
>>                                //Test if next stream is an option
>>                                for ($iterator->next(); $iterator->valid(); $iterator->next() ) {
>>                                        if ( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
>>                                                //echo "$currOpt $opts\n";
>>                                                //$handler($currOpt, $opts);
>>                                                $currOpt = $opts;
>>                                        }
>>                                        var_dump($iterator->valid());
>>                                }
>>                        }//End if
>>                        //echo "$currOpt $opts\n";
>>                        //$handler($currOpt, $opts);
>>                }// End for
>>
>>        //If option is not set first.
>>        } else {
>>                //Try other approach.
>>        }// End if
>> }
>>
>>
>> I've no idea what is going on.
>> Please help.
>>
>>
>> Regards,
>> Panguin
>>
>> --
>> 筆使文富,卻使人窮。
>
>
>
> --
> Regards,
> Panguin

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux