1320 posts
  • Elite Author
  • Has been a member for 5-6 years
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a Free File of the Month
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
+1 more
fuelthemes says

I am using a foreach function similar to one below

foreach (do_something_that_returns_an_array() as $key => $val)
{
    echo "    Inside the loop: $key => $value";

}

However this does not work on my localhost, but works on my remote server.

I received some support requests that buyers cant get it to work either.

Any help is appreciated!

2024 posts
  • Elite Author
  • Author had a Free File of the Month
  • Has been a member for 4-5 years
  • Austria
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Envato Studio (Microlancer) Beta Tester
+3 more
revaxarts says
I wouldn’t call a function within the loop rather this:
array = do_something_that_returns_an_array();
foreach (array as $key => $val)
{
    echo "    Inside the loop: $key => $value";

}

This is maybe not the solution to your problem though

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

Is $val different than $value?

It should simply be like this:

foreach (do_something_that_returns_an_array() as $key => $val)
{
    echo "    Inside the loop: $val";

}

Parker

1320 posts
  • Elite Author
  • Has been a member for 5-6 years
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a Free File of the Month
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
+1 more
fuelthemes says

Thanks revaxart, but that does not solve it either.

@P&k, thats just a typo :)

What happens is, on my localhost, page stops loading, no erros or anything.

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says
turkhitbox said
Thanks revaxart, but that does not solve it either.

@P&k, thats just a typo :)

What happens is, on my localhost, page stops loading, no erros or anything.

Well, in any case you already take the $val, so no need to do that again $key => $value, just use $val.

Do you have the debug mode set to true in the wp-config.php?

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

Another way is to check what the array has inside, maybe the functions doesn’t return it right:

$arr = do_something_that_returns_an_array();
print_r($arr);
foreach ($arr as $key => $val)
{
    echo "    Inside the loop: $val";

}

Parker

2024 posts
  • Elite Author
  • Author had a Free File of the Month
  • Has been a member for 4-5 years
  • Austria
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Envato Studio (Microlancer) Beta Tester
+3 more
revaxarts says
ParkerAndKent said
Is $val different than $value?

It should simply be like this:

foreach (do_something_that_returns_an_array() as $key => $val)
{
    echo "    Inside the loop: $val";

}
Parker
Yes, this isn’t possible:
$key => $value
3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says
revaxarts said
ParkerAndKent said
Is $val different than $value?

It should simply be like this:

foreach (do_something_that_returns_an_array() as $key => $val)
{
    echo "    Inside the loop: $val";

}
Parker
Yes, this isn’t possible:
$key => $value

Yes, really impossible :D

When you make a foreach on a multidimensional array, you already extract both the key and the value, so trying to extract the vause from the key as it was a key is absolutely wrong.

You maybe want to understand how foreach loops work ;)

http://php.net/manual/en/control-structures.foreach.php
1320 posts
  • Elite Author
  • Has been a member for 5-6 years
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a Free File of the Month
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
+1 more
fuelthemes says

That’s possible and it works :)

I don’t why it does not work on my localhost. Even the var_dump returns an array: var_dump(do_something_that_returns_an_array() ;

this loop just doesn’t run on my localhost

foreach (do_something_that_returns_an_array() as $key => $val)

499 posts
  • Canada
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Has been a member for 5-6 years
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
TutelageSystems says

So just to test why not try a for loop?

$varArray = do_something_that_returns_an_array();

for($i=0; $i <= count(varArray); $i++)
{
  print $i .' = '. $varArray[$i];
}

This “should” print the array just like the for each, and won’t hurt to try.

by
by
by
by
by
by