law Posted May 4, 2009 Share Posted May 4, 2009 $string = "Hello this is a string"; $string = explode(" ", $string); foreach ($string as $key => &$a){ echo $a[0]; } outputs : "Htias" <-- The fist letter of each key from the array I want $a to contain the entire array piece. $a[0] == $string[0] . Not just the first letter of the array piece. Anyone know what I'm doing wrong. I could not find this issue in the PHP manual, but 80% of that manual is far beyond my comprehension. Should I just do a for loop and manually advance the pointer? Quote Link to comment https://forums.phpfreaks.com/topic/156798-solved-foreach-has-defeated-me/ Share on other sites More sharing options...
Maq Posted May 4, 2009 Share Posted May 4, 2009 Just echo out the value, you're already looping through the result from explode. echo $a; Quote Link to comment https://forums.phpfreaks.com/topic/156798-solved-foreach-has-defeated-me/#findComment-825791 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Well a string is an array of characters. $e = 'abc'; echo $e[0]; That gives "a". You should just use $a instead of $a[0]. Also, why do you have a reference & next to $a? Wouldn't the foreach loop advance to the next array entry for you? Quote Link to comment https://forums.phpfreaks.com/topic/156798-solved-foreach-has-defeated-me/#findComment-825792 Share on other sites More sharing options...
law Posted May 4, 2009 Author Share Posted May 4, 2009 wow.. i hate when an answer is that simple! Thank you guys.. As Per the & question: I thought i read in the manual that "&" created a pointer to the original $string variable rather then creating a duplicate string. In my actual code my string is a few thousand characters long. I was trying to minimize the impact on my servers ram/cpu. Am I really far off on that concept? I would love any thoughts on how to do this? Because this forloop will be running every 5 seconds to sort massive amounts of data. Quote Link to comment https://forums.phpfreaks.com/topic/156798-solved-foreach-has-defeated-me/#findComment-825801 Share on other sites More sharing options...
lispwriter Posted May 4, 2009 Share Posted May 4, 2009 since you exploded by spaces, the variable $string is an array of 5 elements, each array element is a single word from your initial string. so $string[0] would hold the value "Hello". within the foreach loop, $a holds the value of a single element from your $string array, one at a time. so on the first iteration $a holds the value "Hello". on the second it holds "this" and so on. also in a foreach loop, unless you need the key values, you can simplify what you wrote like this: foreach($string as $a) { ... } also, as mentioned, it's probably not necessary to use the & operator to pass those values by reference however it couldn't hurt if you're dealing with massive amounts of data as you said. hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/156798-solved-foreach-has-defeated-me/#findComment-825803 Share on other sites More sharing options...
law Posted May 4, 2009 Author Share Posted May 4, 2009 wow thank you for the time to put it into plain english LispWriter .. I really appreciate the help guys Quote Link to comment https://forums.phpfreaks.com/topic/156798-solved-foreach-has-defeated-me/#findComment-825837 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.