Hi!
This:
function foo()
{
$a = array('a' => 'b', 'c' => 'd');
return array_keys($a);
}
while ($a = each(foo()))
{
echo 'dummy';
}
produces an infinite loop. Probably since the result of array_key doesn't
support an array pointer or so, I don't know how this is internally
implemented.
Shouldn't this be considered as a bug? Shouldn't be the resulst of array_key
walkable with each?
Regards,
André 7 1413
André Hänsel wrote: Hi!
This:
function foo() { $a = array('a' => 'b', 'c' => 'd'); return array_keys($a); } while ($a = each(foo())) { echo 'dummy'; }
produces an infinite loop. Probably since the result of array_key doesn't support an array pointer or so, I don't know how this is internally implemented.
Shouldn't this be considered as a bug? Shouldn't be the resulst of array_key walkable with each?
It's a bug alright--yours. foo() will return a fresh array each time,
so the condition is always true.
On Thu, 12 Jan 2006 03:45:30 +0100, André Hänsel wrote: function foo() { $a = array('a' => 'b', 'c' => 'd'); return array_keys($a); } while ($a = each(foo())) { echo 'dummy'; }
Andre, the problem is not in the pointer, the problem is in the
loop. The function foo() will be called each time the loop condition
is checked, producing always a new array and a new set of keys.
Here is a code that does work:
$ cat /tmp/ttt;
<?
function foo() {
$a = array('a' => 'b', 'c' => 'd');
return array_keys($a);
}
$b=foo();
while ($a = each($b))
{
echo "dummy\n";
}
?>
$ php /tmp/ttt
dummy
dummy
-- http://www.mgogala.com
André Hänsel wrote: produces an infinite loop. Probably since the result of array_key doesn't support an array pointer or so, I don't know how this is internally implemented.
I should add that there is a real bug in PHP 4.4.1 that causes infinite
loop, shown in the following example.
<?
$array = array(1, 2, 3);
function loop(&$array) {
while(($key = key($array)) !== null) {
echo "Hello $key<br>";
next($array);
}
}
loop($array);
?>
Chung Leong wrote: André Hänsel wrote: Hi!
This:
function foo() { $a = array('a' => 'b', 'c' => 'd'); return array_keys($a); } while ($a = each(foo())) { echo 'dummy'; }
produces an infinite loop. Probably since the result of array_key doesn't support an array pointer or so, I don't know how this is internally implemented.
Shouldn't this be considered as a bug? Shouldn't be the resulst of array_key walkable with each?
It's a bug alright--yours. foo() will return a fresh array each time, so the condition is always true.
Oh, I see.
Is there any solution that supports the "while ($a = each(foo()))"
construction?
Caching the result in a static variable?
André Hänsel said the following on 15/01/2006 23:50: Chung Leong wrote: André Hänsel wrote: Hi!
This:
function foo() { $a = array('a' => 'b', 'c' => 'd'); return array_keys($a); } while ($a = each(foo())) { echo 'dummy'; }
produces an infinite loop. Probably since the result of array_key doesn't support an array pointer or so, I don't know how this is internally implemented.
Shouldn't this be considered as a bug? Shouldn't be the resulst of array_key walkable with each? It's a bug alright--yours. foo() will return a fresh array each time, so the condition is always true.
Oh, I see.
Is there any solution that supports the "while ($a = each(foo()))" construction? Caching the result in a static variable?
I guess you could do:
function &foo()
{
static $s = null;
if (null == $s)
{
$a = array('a' => 'b', 'c' => 'd');
$s = array_keys($a);
}
return $s;
}
while ($a = each(foo()))
{
echo 'dummy';
}
Although quite why you would want to do something so perverse, I've no
idea...
You'd be better off writing a custom storage class that implements the
Iterator interface.
--
Oli
I thought the problem was that each() will return a fresh array each time
because of foo(). each() always adds to the array foo() returns.
I also thought a better solution would be to use a foreach lop instead of
while.
foreach (foo() as $key) {
echo "dummy";
}
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
André Hänsel wrote: Hi!
This:
function foo() { $a = array('a' => 'b', 'c' => 'd'); return array_keys($a); } while ($a = each(foo())) { echo 'dummy'; }
produces an infinite loop. Probably since the result of array_key doesn't support an array pointer or so, I don't know how this is internally implemented.
Shouldn't this be considered as a bug? Shouldn't be the resulst of array_key walkable with each?
It's a bug alright--yours. foo() will return a fresh array each time,
so the condition is always true.
Jim Michaels wrote: I thought the problem was that each() will return a fresh array each time because of foo(). each() always adds to the array foo() returns. I also thought a better solution would be to use a foreach lop instead of while.
each() is a real function. It isn't a language construct, unlike a
foreach() statement. foo() then each() will be called in each
iteration. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sylvain Hellegouarch |
last post by:
Hi,
A bit off topic.
I just wondered what was your feeling when you were coding with Python.
I have beebn coding with different languages and the only that has given
me the will to invent or...
|
by: Magnus Warker |
last post by:
Hi,
I want to traverse an (associative) array, starting from its current
position, until a certain element is reached. Then, in certain cases, I
want to be able to reset the current position of...
|
by: josulliv101 |
last post by:
Hi,
I have a very basic question. What is meant by the term "internal
function"? What makes it "internal"?
-newbie
|
by: black |
last post by:
hi all~
i wrote some functions for copying and moving files caz' i didnt find
concret functions within the doc. but i think these operations are
simple and important so there may be some...
|
by: venky |
last post by:
Hi all,
I'm new to group n c. i want to know in detail about volatile qualifier
and pointer to functions with examples.
thank you
|
by: Vic Spainhower |
last post by:
Hello,
I am exporting a table from MS Access which is then imported into a MySQL
table. I am using a TransferText command in Access to export the data to a
TAB delimeted file. Then using the...
|
by: tea-jay |
last post by:
hi again ..
well i do write the code but it doesnt work
i really dont what to do ?
this is my code
#include<iostream>
#include<cstdlib>
#include<cmath>
|
by: Janwillem |
last post by:
I want to make numerical functions that can be called from python.
I am programming in pascal the last few decades so I had a look at
"python for delphi" (P4D). The demo09 gives as example ...
|
by: Jeff |
last post by:
Lets say we have an array:
$array='apples';
$array='orange';
$array='lemon';
echo $array; // orange
Now, lets say we want to insert "grape", between apples and orange. How
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |