Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.