I have an array that looks like this:
$array = array(
array(
"http://google.com",
"Google"
),
array(
"http://yahoo.com",
"Yahoo"
)
);
What is the simplest way to loop through it. Something like:
foreach ($array as $arr) {
// help
}
EDIT: How do I target keys, for example, I want to do:
foreach ($array as $arr) {
echo '<a href" $key1 ">';
echo ' $key2 </a>';
}
Use nested
foreach()
because it is 2D array. Example hereIn first
foreach()
$val
is also an array. So a nestedforeach()
is used. In secondforeach()
$v
is string.Updated according to your demand
The way to loop through is,
Use the first foreach loop without
{ }
for the simplest use.That can be the most simple method to use a nested array as per your request.
For your edited question.
Wrong declaration of array for using key.
And then, use the following.
This doesn't slow down your server's performance.
First modify your variable like this:
then you can loop like this:
In order to echo out the bits you have to select their index in each array -
Here is an example.
The easiest way to loop through it is:
EDIT:
If you know that your array will have always only two indexes then you can try this: