Vitamin Posted November 2, 2010 Share Posted November 2, 2010 I'm trying to build an array in a foreach loop, but when I got to echo the results it only displays the first character of the string, but if I do a print_r on the array it show the data fine. $msgColor1 = array(); foreach ($playerName1 as $value) { $msgColor1['name'] .= $value[$i]; $i = $i + 1; } print_r show the data but when i do foreach ($msgColor1 as $test) { echo $test['name']; } Only shows the first character in the string Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/ Share on other sites More sharing options...
BlueSkyIS Posted November 2, 2010 Share Posted November 2, 2010 what is supposed to be in the array? Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129671 Share on other sites More sharing options...
Vitamin Posted November 2, 2010 Author Share Posted November 2, 2010 the name of the person in playerName1 which is a array Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129682 Share on other sites More sharing options...
jcbones Posted November 2, 2010 Share Posted November 2, 2010 $value is probably a string, and not an array. Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129697 Share on other sites More sharing options...
Vitamin Posted November 3, 2010 Author Share Posted November 3, 2010 $value is a string. I want to put that string in an array. Anyone have a good tutorial about building arrays in a loop? I've been looking, but cant really find what I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129749 Share on other sites More sharing options...
jcbones Posted November 3, 2010 Share Posted November 3, 2010 Modify your code to reflect it. $msgColor1 = array(); foreach ($playerName1 as $value) { $msgColor1['name'] .= $value[$i]; //<- you are referencing value as an array, which would split the string by characters. remove the [$i], and you are good to go. $i = $i + 1; } So it should read. $msgColor1 = array(); foreach ($playerName1 as $value) { $msgColor1['name'] .= $value; $i = $i + 1; } Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129756 Share on other sites More sharing options...
Vitamin Posted November 3, 2010 Author Share Posted November 3, 2010 Oh right on. Thank you so much works like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129758 Share on other sites More sharing options...
marcus Posted November 3, 2010 Share Posted November 3, 2010 Are you trying to store all of the player names in an multi-dimensional array? So that msgColor1 is an array and has an array inside of it with the key named "name" and each value of the array "name" has each of the player names? If that's the case: foreach($playerName1 AS $value){ $msgColor1['name'][] = $value; } And calling print_r on msgColor1 will give you an output of something similar to: Array ( [name] => Array ( [0] => johnny [1] => bob [2] => malcolm ) ) Quote Link to comment https://forums.phpfreaks.com/topic/217598-build-array-in-foreac-loop/#findComment-1129759 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.