Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding another condition to a foreach loop (PHP)
    text
    copied!<p>I have two arrays of data to work with, and I'm trying to create a loop that will utilize them both like so.</p> <p>Array A is a list of keywords. Array B is a list of city names.</p> <p>I'd like for the following foreach loop to take each keyword and combine it with a city name.When each keyword has been matched successfully to every city in Array B, I'd like the loop to continue on through the Array A values and continue the matching.</p> <p>It seems simple enough, but using the code below, I have been unable to get it working right. The loop works successfully in grabbing one value from the list of keywords, but it then simply adds every value from array B to that keyword and loops through that way.</p> <p>My code can be seen below:</p> <pre><code>&lt;?php foreach ($keyword_list as $key =&gt; $value) { $keyword = strtolower($value); //REMOVE COMMAS $keyword = str_replace(',', '', $keyword); //ADD HYPHENS $keyword = str_replace(' ', '-', $keyword); for ($i = 0; $i &lt; count($content); $i++) { $content[$i] = trim($content[$i]); //RE-REPLACE SINGLE QUOTES TO ESCAPED VERSIONS FOR INCLUSION IN HTML DOC (PHP ARRAY) $content[$i] = str_replace("'", "\'", $content[$i]); } $file = strtolower(str_replace(' ', '-', $businessName . '-' . $businessPhone)); $file .= '-' . $keyword; foreach ($city_list as $key2=&gt;$value2) { $file .='-' . $cityword; } $filename = $file . '.php'; } ?&gt; </code></pre> <p>So with that code above, the following would be produced as an example...</p> <pre><code>Google-5555555555-Keyword Here-CityNameOne-CityNameTwo-CityNameThree.php </code></pre> <p>Instead, I'd like it to work as such...</p> <pre><code>Google-5555555555-Keyword Here-CityNameOne.php Google-5555555555-Keyword Here-CityNameTwo.php Google-5555555555-Keyword Here-CityNameThree.php </code></pre> <p>I thought this would be easy but I'm learning as I go and I'm stumped by what could be causing this issue. If there is anything that I can do to help in resolving it, please advise! I do wish to learn how to do this properly so I'm simply looking for any guidance at all.</p> <p>Thank you very much for taking the time to read my question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload