copying contents of one array into another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

copying contents of one array into another

Post 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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: copying contents of one array into another

Post 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);
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: copying contents of one array into another

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: copying contents of one array into another

Post 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";
}
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: copying contents of one array into another

Post by swetha »

thanks,
this worked
Post Reply