soma56 Posted October 20, 2010 Share Posted October 20, 2010 I can't seem to find an answer on this one. I've figured out how to add key/value pairs, add to the beginning and end - but the question here is how do you append to each value within an array? Let's say I wanted to add the word 'go'. From: Array ( [0] => Bruins [1] => Rangers [2] => Leafs ) To: Array ( [0] => GoBruins [1] => GoRangers [2] => GoLeafs ) Seems simple enough but Google isn't being friendly today. Quote Link to comment https://forums.phpfreaks.com/topic/216344-how-would-anyone-append-to-every-value-within-an-array/ Share on other sites More sharing options...
trq Posted October 20, 2010 Share Posted October 20, 2010 Assuming your array is $arr. foreach ($arr as $k => $v) { $arr[$k] = 'Go' . $v; } Quote Link to comment https://forums.phpfreaks.com/topic/216344-how-would-anyone-append-to-every-value-within-an-array/#findComment-1124269 Share on other sites More sharing options...
soma56 Posted October 20, 2010 Author Share Posted October 20, 2010 Hey Thorpe, it's nice to see you're still hear helping people like me out. I thought of that but then how would I place all the $v back into the array? I hope that makes sense. The reason is because I have to combine it with another one... <?PHP foreach ($arr as $k => $v) { $arr[$k] = 'go' . $v; } $combine = array_merge((array)$arr, (array)$anotherarray); print_r($combine); ?> Quote Link to comment https://forums.phpfreaks.com/topic/216344-how-would-anyone-append-to-every-value-within-an-array/#findComment-1124273 Share on other sites More sharing options...
trq Posted October 20, 2010 Share Posted October 20, 2010 I thought of that but then how would I place all the $v back into the array? The code I posted does do that. Quote Link to comment https://forums.phpfreaks.com/topic/216344-how-would-anyone-append-to-every-value-within-an-array/#findComment-1124288 Share on other sites More sharing options...
Pikachu2000 Posted October 20, 2010 Share Posted October 20, 2010 There's only one that's worth appending to, so you really don't need a loop at all $v= 'Stars'; $v = 'Go' . $v; Quote Link to comment https://forums.phpfreaks.com/topic/216344-how-would-anyone-append-to-every-value-within-an-array/#findComment-1124415 Share on other sites More sharing options...
soma56 Posted October 20, 2010 Author Share Posted October 20, 2010 I rushed through it - yes it worked. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216344-how-would-anyone-append-to-every-value-within-an-array/#findComment-1124418 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.