0

I would really appreciate your help.

I want to use an additional array with foreach.

Currently it looks like this and works perfectly.

<?php

$listcity = array("Negril" => "negril",
"Kingston" => "kingston",
"Ocho Rios" => "ocho-rios");

foreach ($listcity as $cityname => $citylink) {
    echo "<li data-name=\"$cityname Hotels\"><a href=\"https://example.com/hotels/$citylink\">$cityname</a></li>";
}
?>

Now I want to add a span with the following numbers:

 $distancemiles = array("Negril" => "25",
  "Kingston" => "10",
  "Ocho Rios" => "67");

I want the final echo to be the following:

<li data-name=\"$cityname Hotels\"><a href=\"https://example/hotels/$citylink\">$cityname</a><span>(HERE THE DISTANCE IN MILES miles)</span></li>

so that the browser outputs for instance:

Negril (50 miles)

So I would like to know how I can combine my three sources of data:

$cityname --> Should be the name of the city
$citylink --> Should be the link ending
$distancetocity --> Should be the distance

Would someone help me with this? I've tried very hard to make it work and read through all kinds of online tutorials, but I fail every time.

Best,

Max

1
  • how do you get the array ? Commented Mar 17, 2017 at 16:51

5 Answers 5

1

You can find distance of city by it name. The $distancemiles[$cityname] return distance of city.

foreach ($listcity as $cityname => $citylink){
    echo "<li data-name=\"$cityname Hotels\">
        <a href=\"https://example.com/hotels/$citylink\">$cityname</a>
        <span>$distancemiles[$cityname]</span>
    </li>";
}

See result of code in demo

Sign up to request clarification or add additional context in comments.

Comments

1
<?php

$listcity = array("Negril" => "negril",
"Kingston" => "kingston",
"Ocho Rios" => "ocho-rios");

$distancemiles = array("Negril" => "25",
  "Kingston" => "10",
  "Ocho Rios" => "67");

foreach ($listcity as $cityname => $citylink) {
    echo "<li data-name=\"$cityname Hotels\"><a href=\"https://example/hotels/$citylink\">$cityname</a><span>($distancemiles[$cityname])</span></li>";
}
?>

You can look up the value of the miles using your distance array.

Your array list city array has structure cityname->linkname your distance array has structure cityname->distanceInMiles so you can use the city name from your for loop to look up the distance at the index of the cityname.

Comments

1

Then you can do:

echo "<li data-name=\"$cityname Hotels\"><a href=\"https://example.com/hotels/$citylink\">$cityname</a><span>{$distancemiles[$cityname]}</span></li>";

Comments

1

using arrray_merge_recursive :

merge your array into one array, then access it as follows:

$listcity = array("Negril" => "negril",
"Kingston" => "kingston",
"Ocho Rios" => "ocho-rios");

$distancemiles = array("Negril" => "25",
  "Kingston" => "10",
  "Ocho Rios" => "67");

$oneArray = array_merge_recursive($listcity, $distancemiles);


foreach ($oneArray as $cityname => $citylink) {
    echo "<li data-name=\"$cityname Hotels\"><a href=\"https://example/hotels/".$citylink[0]."\">$cityname</a><span>".$citylink[1]."</span></li>";
}

live example: https://3v4l.org/Q0VDn

Comments

0

If you add a reference to the $distancemiles array with $cityname as key (like $distancemiles[$cityname]) you can get the miles within the foreach loop.

$listcity = array(
    "Negril" => "negril",
    "Kingston" => "kingston",
    "Ocho Rios" => "ocho-rios");

$distancemiles = array("Negril" => "25",
  "Kingston" => "10",
  "Ocho Rios" => "67");

foreach ($listcity as $cityname => $citylink) {
    echo "<li data-name=\"$cityname Hotels\"><a href=\"https://example.com/hotels/$citylink\">$cityname</a><span>$distancemiles[$cityname]</span></li>";
}

// Negril25
// Kingston10
// Ocho Rios67

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.