Jump to content

[SOLVED] Help Combining Two Strings


JSHINER

Recommended Posts

<?php
foreach($page['towns'] as $x) {
$town = strtolower(str_replace(" ", "", $x['name']));
$state = $x['state'];
$townState = $town . $state;
$theArray[] = $townState;
}
?>

 

I'm trying to use the above code to add items to an array. It works with other foreach statements but this one where I try to combine two strings does not work. What is wrong with it?

Link to comment
https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/
Share on other sites

There's 29,000+ towns in the US so it's pretty long. But here's part of it...

 

Array ( [0] => Array ( [name] => Town [state] => St ) [1] => Array ( [name] => Adak [state] => AK ) [2] => Array ( [name] => Akiachak [state] => AK ) [3] => Array ( [name] => Akiak [state] => AK ) [4] => Array ( [name] => Akutan [state] => AK ) [5] => Array ( [name] => Alakanuk [state] => AK ) [6] => Array ( [name] => Aleknagik [state] => AK ) [7] => Array ( [name] => Allakaket [state] => AK ) [8] => Array ( [name] => Ambler [state] => AK ) [9] => Array ( [name] => Anaktuvuk Pass [state] => AK ) [10] => Array ( [name] => Anchor Point [state] => AK ) [11] => Array ( [name] => Anchorage [state] => AK ) [12] => Array ( [name] => Anderson [state] => AK ) [13] => Array ( [name] => Angoon [state] => AK ) [14] => Array ( [name] => Aniak [state] => AK )

<?php
foreach($page['towns'] as $x) {
   $town = strtolower(str_replace(" ", "", $x['name']));
   $state = $x['state'];
   $townState = $town . $state;
   echo $town, $state, '<br>';
//   $theArray[] = $townState;
}
?>

 

The above returns:

 

townSt

adakAK

akiachakAK

akiakAK

akutanAK

alakanukAK

aleknagikAK

allakaketAK

amblerAK

anaktuvukpassAK

anchorpointAK

anchorageAK

andersonAK

angoonAK

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.