0

Hello there,

I am trying to add more than 2 foreach statements. Please help me.

My statement -

foreach (array_combine($image, $name) as $pic => $title) {
}

It cant be like -

foreach (array_combine($image, $page, $name) as $pic => $id => $title) {
}
2
Contributors
1
Reply
10
Views
1 Year
Discussion Span
Last Post by cereal
0

Hi,

the foreach construct only accept these syntaxes:

foreach (array_expression as $value)

And:

foreach (array_expression as $key => $value)

it seems you're trying to extract two keys, it won't work. The array_combine() function does accepts only two arguments, you're submitting three arguments (arrays) so it will fail.

If these are multidimensional arrays the you could use array_merge() and write:

$data = array_merge($image, $page, $name);
foreach($data as $key => $value) {

But consider the following statements from the documentation:

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator

And:

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

Documentation:

Edited by cereal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.