- Author was Featured
- Has been a member for 4-5 years
- Author had a Free File of the Month
- Sold between 100 000 and 250 000 dollars
- Bought between 10 and 49 items
- Europe
- Exclusive Author
- Referred between 200 and 499 users
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!
- Microlancer Beta Tester
- Author had a Free File of the Month
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Austria
- Exclusive Author
- Referred between 200 and 499 users
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
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
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
- Author was Featured
- Has been a member for 4-5 years
- Author had a Free File of the Month
- Sold between 100 000 and 250 000 dollars
- Bought between 10 and 49 items
- Europe
- Exclusive Author
- Referred between 200 and 499 users
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.
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
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?
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
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
- Microlancer Beta Tester
- Author had a Free File of the Month
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Austria
- Exclusive Author
- Referred between 200 and 499 users
ParkerAndKent saidYes, this isn’t possible:
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
$key => $value
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
revaxarts said
ParkerAndKent saidYes, this isn’t possible:
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$key => $value
Yes, really impossible 
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 
- Author was Featured
- Has been a member for 4-5 years
- Author had a Free File of the Month
- Sold between 100 000 and 250 000 dollars
- Bought between 10 and 49 items
- Europe
- Exclusive Author
- Referred between 200 and 499 users
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)
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.
