JSHINER Posted December 20, 2008 Share Posted December 20, 2008 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/ Share on other sites More sharing options...
DarkWater Posted December 20, 2008 Share Posted December 20, 2008 Can I see a print_r() on $page['towns']? Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720351 Share on other sites More sharing options...
JSHINER Posted December 20, 2008 Author Share Posted December 20, 2008 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720354 Share on other sites More sharing options...
waynew Posted December 20, 2008 Share Posted December 20, 2008 Could you echo out $town and $state? Also, do you know what error reporting you have? If you can, set it to E ALL. Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720362 Share on other sites More sharing options...
JSHINER Posted December 20, 2008 Author Share Posted December 20, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720363 Share on other sites More sharing options...
waynew Posted December 20, 2008 Share Posted December 20, 2008 And what ends up in $theArray? Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720364 Share on other sites More sharing options...
JSHINER Posted December 20, 2008 Author Share Posted December 20, 2008 Nothing ends up in the array it errors out. Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720365 Share on other sites More sharing options...
waynew Posted December 20, 2008 Share Posted December 20, 2008 What's the error? Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720366 Share on other sites More sharing options...
waynew Posted December 20, 2008 Share Posted December 20, 2008 Try: array_push($theArray,$townState); instead of... $theArray[] = $townState; Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720367 Share on other sites More sharing options...
JSHINER Posted December 20, 2008 Author Share Posted December 20, 2008 Same thing with array_push($theArray,$townState); Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720368 Share on other sites More sharing options...
wildteen88 Posted December 20, 2008 Share Posted December 20, 2008 Same thing with array_push($theArray,$townState); Then post the error(s) you are getting. Posting "it errors out" isn't helpful Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720369 Share on other sites More sharing options...
JSHINER Posted December 20, 2008 Author Share Posted December 20, 2008 Error: Blank white screen. Using error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720374 Share on other sites More sharing options...
wildteen88 Posted December 20, 2008 Share Posted December 20, 2008 Error: Blank white screen. Using error_reporting(E_ALL); Ensure display_errors is enabled too: error_reporting(E_ALL); ini_set('display_errors', 'On'); Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720377 Share on other sites More sharing options...
JSHINER Posted December 20, 2008 Author Share Posted December 20, 2008 Was a memory issue - set ini_set('memory_limit', '128M'); - works great. Sorry about that everyone... should have had display_errors on from the start :-\ Quote Link to comment https://forums.phpfreaks.com/topic/137829-solved-help-combining-two-strings/#findComment-720379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.