1386 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Has sold $250,000+ on Envato Market
+6 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!

2071 posts
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Provided great feedback to improve the user experience on Envato Market
  • Interviewed on an Envato blog
+9 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
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 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

1386 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Has sold $250,000+ on Envato Market
+6 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
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 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
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 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

2071 posts
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Provided great feedback to improve the user experience on Envato Market
  • Interviewed on an Envato blog
+9 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
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 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
1386 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Has sold $250,000+ on Envato Market
+6 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
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in Canada
+2 more
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