1468 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 6 years
  • Has sold $250,000+ on Envato Market
+5 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!

2188 posts
  • Located in Europe
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Participated in a focus group or interview to improve the user experience on Envato Market
+10 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

1468 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 6 years
  • Has sold $250,000+ on Envato Market
+5 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

2188 posts
  • Located in Europe
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Participated in a focus group or interview to improve the user experience on Envato Market
+10 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
1468 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 6 years
  • Has sold $250,000+ on Envato Market
+5 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.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by