Page 1 of 1
copying contents of one array into another
Posted: Thu Oct 23, 2008 3:07 am
by swetha
Code: Select all
function sorttext($str)
{
$str="First\nSecond\nThird";
explode (" ",$str);
sort($str)
}
{
$constr=sorttext($origtext);
echo $constr;
}
assume i have the above string in $str.May aim is to sort the contents in a textarea which
are on different lines.$origtext is the contents of the textarea.how do i return the sorted lines into an array?thanks for any help.
Re: copying contents of one array into another
Posted: Thu Oct 23, 2008 3:15 am
by papa
Code: Select all
$origtext="First\nSecond\nThird";
function sorttext($str)
{
$str = explode("\n", $str);
sort($str);
return $str;
}
$constr = sorttext($origtext);
print_r($constr);
Re: copying contents of one array into another
Posted: Thu Oct 23, 2008 4:06 am
by swetha
hi,
thanks for the reply.im getting the output as follows.
Array
(
[0] => aaaaaa
[1] => bbbbbb
[2] => wrwetw
[3] => zzzzzzzzz
)
But i need the output as follows :
aaaaa
bbbbb
wrwetw
zzzzzzzzz
i.e the output needs to be printed as above using echo statement and not print_r.
pls help.thanks
Re: copying contents of one array into another
Posted: Thu Oct 23, 2008 4:19 am
by papa
Either skip the entire function and write: echo nl2br($origtext);
or
Instead of print_r($constr);...
Code: Select all
foreach($constr as $line) {
echo $line."<br />\n";
}
Re: copying contents of one array into another
Posted: Thu Oct 23, 2008 5:44 am
by swetha
thanks,
this worked