![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| > function trim_data($formdata) > { > foreach ($formdata as $key => $value) > { > $key = trim($key); > $value = trim($value); > } > return $formdata; > } > > Can anyone spare the odd microsecond to point me in the right direction? You're not doing anything with the trimmed values. Try something like: function trim_data($formdata) { foreach ($formdata as $key => $value) { $newdata[trim($key)] = trim($value); } return $newdata; } -- Roger Harry Nilsson's "The Point!" on DVD http://www.harrynilsson.com/movie-5-1510.html | |||
| Advertisements |
| | #2 | ||
| Roger, Many thanks - that works a treat. I guess I must conclude that it is not possible to change the array elements in a foreach loop? My old method looked logical in the same way that you can say $apple = $apple + 1; or whatever. Anyway, you've set me straight, and I'm most grateful. Regards Graham "Roger Smith" <roger@ns_jadebox.com> wrote in message news:RngRa.1409$d47.154616@twister.tampabay.rr.com ... > > function trim_data($formdata) > > { > > foreach ($formdata as $key => $value) > > { > > $key = trim($key); > > $value = trim($value); > > } > > return $formdata; > > } > > > > Can anyone spare the odd microsecond to point me in the right direction? > > You're not doing anything with the trimmed values. Try something like: > > function trim_data($formdata) > { > foreach ($formdata as $key => $value) > { > $newdata[trim($key)] = trim($value); > } > return $newdata; > } > > -- Roger > Harry Nilsson's "The Point!" on DVD > http://www.harrynilsson.com/movie-5-1510.html > > | |||
| | #3 | ||
| In this instance it was only the values that I wanted to change, but I thought it best to make the function more general. If you've got the time, I'd like to understand how you could in fact change the values (I'm pretty new to this, so every bit of learning helps), but if not don't worry - you've already solved my problem! Many thanks again. Regards Graham "Roger Smith" <roger@ns_jadebox.com> wrote in message news:W3hRa.1478$d47.158367@twister.tampabay.rr.com ... > > I guess I must conclude that it is not possible to change the array > elements > > in a foreach loop? My old method looked logical in the same way that you > > can say $apple = $apple + 1; or whatever. > > You could change the values, but changing the keys would be really messy. > You'd have to add a new element and drop the old one inside the loop. I'm > not sure how the "foreach" construction would react to that. > > -- Roger > > | |||
| | #4 | ||
| It looks a lot neater that way. Thanks again, I've learned some very useful stuff from you. Regards G "Roger Smith" <roger@ns_jadebox.com> wrote in message news:TNlRa.119794$ic1.2358118@twister.tampabay.rr. com... > > If you've got the time, I'd like to understand how you could in fact > change > > the values (I'm pretty new to this, so every bit of learning helps), but > if > > not don't worry - you've already solved my problem! > > You could put the following in the loop: > > $formdata[$key] = trim($value) > > -- Roger > http://www.exclaimer.com/ > More than 16 Made-up Words > > | |||
| | #5 | ||
| Martin, I didn't know that was how foreach worked. It makes sense now you've pointed it out to me. Thanks for getting involved in the thread. Regards Graham "Martin C. Petersen" <mcp@phys.au.dk> wrote in message news:3f15a57b$0$83045$edfadb0f@dtext01.news.tele.d k... > > I guess I must conclude that it is not possible to change the array > elements > > in a foreach loop? My old method looked logical in the same way that you > > can say $apple = $apple + 1; or whatever. > foreach ($array as $k => $v) puts a copy of the actual array-element into > $v, so modifying $v does not effect the content of $array[$k]. > > > Martin > > | |||
| Featured Websites | ||||
|
![]() |
| Tags: array, data, trimming |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting desperate - can't get data out of array with string index | lawrence | PHP | 2 | 07-01-2007 3:18 PM |
| How do I sort This array? | Karl McAuley | PHP | 1 | 07-01-2007 3:18 PM |
| How to delete an array key??? | Barton | PHP | 0 | 07-01-2007 3:12 PM |
| Please help - Cracking elevation data format in data file | Kevin Fishburne | Software Programming | 10 | 06-12-2007 11:31 PM |
| Featured Websites | ||||
|