473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Nested foreach loop over same array

I'm using next snippet:

$somearray = array(...);

foreach($somearray as $item1) {
foreach($item1 as $item2) {
// ... do something ...
}
}

....and I'm getting next error:
Invalid argument supplied for foreach()

on second foreach.

This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?
Tnx.
Jul 9 '08 #1
5 5224

Ivan S schreef:
I'm using next snippet:

$somearray = array(...);

foreach($somearray as $item1) {
foreach($item1 as $item2) {
// ... do something ...
}
}

...and I'm getting next error:
Invalid argument supplied for foreach()

on second foreach.
Hi Ivan,

Nothing wrong with your code.
The two foreach constructs should be no problem.
I expect that the original array isn't containing what you expect it to
contain.
Since you use a foreach on every value in the $somearray, this array
should contain of arrays itself.

Test that like this:
echo "<pre>";
print_r($somearray);
echo "</pre>";
exit;

Regards,
Erwin Moller

>
This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?
Tnx.
Jul 9 '08 #2
Ivan S schrieb:
I'm using next snippet:

$somearray = array(...);

foreach($somearray as $item1) {
foreach($item1 as $item2) {
// ... do something ...
}
}

...and I'm getting next error:
Invalid argument supplied for foreach()

on second foreach.

This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?
Tnx.
The function is_array() exists :-)

Jul 10 '08 #3
On 9 srp, 12:06, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
Ivan S schreef:
I'm using next snippet:
$somearray = array(...);
foreach($somearray as $item1) {
* * * foreach($item1 as $item2) {
* * * * * * // ... do something ...
* * *}
}
...and I'm getting next error:
Invalid argument supplied for foreach()
on second foreach.

Hi Ivan,

Nothing wrong with your code.
The two foreach constructs should be no problem.
I expect that the original array isn't containing what you expect it to
contain.
Since you use a foreach on every value in the $somearray, this array
should contain of arrays itself.

Test that like *this:
echo "<pre>";
print_r($somearray);
echo "</pre>";
exit;

Regards,
Erwin Moller
This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?
Tnx.

Hi Erwin. :)

Your advice helped, some items in $somearray weren't arrays it self,
that was reason why i got errors reported.

Thanks for your help.
Jul 10 '08 #4
On 10 srp, 05:25, Olaf Schinkel <tr...@schinkel.tvwrote:
Ivan S schrieb:
I'm using next snippet:
$somearray = array(...);
foreach($somearray as $item1) {
* * * foreach($item1 as $item2) {
* * * * * * // ... do something ...
* * *}
}
...and I'm getting next error:
Invalid argument supplied for foreach()
on second foreach.
This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?
Tnx.

The function is_array() exists :-)
I know that...I wasn't aware that my input array could contain
unexpected values (such as non array values).
Jul 10 '08 #5

"Ivan S" <iv*********@gmail.comwrote in message
news:f0**********************************@i76g2000 hsf.googlegroups.com...
On 10 srp, 05:25, Olaf Schinkel <tr...@schinkel.tvwrote:
Ivan S schrieb:
I'm using next snippet:
$somearray = array(...);
foreach($somearray as $item1) {
foreach($item1 as $item2) {
// ... do something ...
}
}
...and I'm getting next error:
Invalid argument supplied for foreach()
on second foreach.
This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?
Tnx.

The function is_array() exists :-)
I know that...I wasn't aware that my input array could contain
unexpected values (such as non array values).
=========

strictly speaking, avoid the error like this:

$somearray = array(...);
foreach ($somearray as $item1)
{
if (!is_array($item1)){ $item1 = array($item1); }
foreach ($item1 as $item2)
{
// do something...
}
}

that way, if $item1 is not an array but still a usable, otherwise valid,
value then your next foreach will be able to process it normally...based on
whatever '...do something...' is.
Jul 10 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
1
by: levidicom | last post by:
foreach($test as $var1){ foreach($test2 as $var2) { echo '"var1: " . $var1 . "<br>"var2: " . $var2 . "<br> \n"'; }
1
by: levidicom | last post by:
foreach($test as $var1){ foreach($test2 as $var2) { echo '"var1: " . $var1 . "<br>"var2: " . $var2 . "<br> \n"'; }
1
by: TSmith | last post by:
Hey Y'all, I haven't programmed in PHP in quite a while. Could anyone help me with a foreach loop in a nested array? I have a function which returns $stock (for an online pricing catalog). ...
7
by: lawpoop | last post by:
Hello all - Is there a way to get a nested array in a $_POST variable? I have a form where there are several questions, each one corresponding to a database row. On submission of the form, I...
3
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.