1642 posts
  • Elite Author
  • Featured Author
  • 6 Years of Membership
  • Author Level 10
+8 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!

2280 posts
  • Top Monthly Author
  • Weekly Top Seller
  • Community Superstar
  • Industrious API Inventor
+15 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
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 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

1642 posts
  • Elite Author
  • Featured Author
  • 6 Years of Membership
  • Author Level 10
+8 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
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 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
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 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

2280 posts
  • Top Monthly Author
  • Weekly Top Seller
  • Community Superstar
  • Industrious API Inventor
+15 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
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 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
1642 posts
  • Elite Author
  • Featured Author
  • 6 Years of Membership
  • Author Level 10
+8 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
  • Affiliate Level 1
  • Author Level 3
  • Collector Level 2
  • 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