Page 1 of 1
trim array?
Posted: Sun Jul 18, 2010 7:26 am
by edhen
is there a way to trim the white spaces and perform normal string functions on an array without having to call each one individually? eg. i got my register form all named as an array 'required[fname]' etc. yet i'm not sure how to process these all at once which was kinda the main reason for putting them in an array... thanks
Re: trim array?
Posted: Sun Jul 18, 2010 7:45 am
by lenton
Not really sure what you mean by your post but you're probably looking for the function 'foreach()':
Code: Select all
foreach ($arr as $key => $value)
{
trim(required[$key]);
}
Re: trim array?
Posted: Sun Jul 18, 2010 7:58 am
by edhen
thanks... i found a solution just before... i tried that way but i think it was causing the array to trim it as a whole or something (not too sure)
Code: Select all
foreach($_POST['required'] as $id=>$value){
$_POST['required'][$id] = trim($value);
$_POST['required'][$id] = strip_tags($value);
}
this way seemed to work nicely...
Re: trim array?
Posted: Sun Jul 18, 2010 8:04 am
by Gargoyle
note that you are throwing away the return value of trim()...
Re: trim array?
Posted: Sun Jul 18, 2010 8:34 am
by edhen
thanks for that... i got alittle ahead of my self with out thinking... btw Ive been up all night trying to experiment in making my own registration form and process without guides for the first time and only calling for help when im really stuck...
i change the code to this
Code: Select all
foreach($_POST['required'] as $id=>$value){
$value = trim($value);
$_POST['required'][$id] = strip_tags($value);
}
would that be the right way to go about it? as it seems to work now...
Re: trim array?
Posted: Sun Jul 18, 2010 12:04 pm
by AbraCadaver
Use array functions if you don't want to loop:
Code: Select all
$required = array_map('strip_tags', array_map('trim', $_POST['required']));
Re: trim array?
Posted: Sun Jul 18, 2010 12:12 pm
by buckit
AbraCadaver wrote:Use array functions if you don't want to loop:
Code: Select all
$required = array_map('strip_tags', array_map('trim', $_POST['required']));
Ha! was just about to reply with something similar! Saw your replay to one of my posts yesterday about using array functions instead of loops and thought I would give it a go with this one! you beat me to it!

Re: trim array?
Posted: Sun Jul 18, 2010 2:39 pm
by edhen
thanks for the info... i had a glance at array_map on php.net but was hesitant on using it as i was unfamiliar with how exactly it work... but thanks for simplifying it for me in your example will be put to good use...