another question

hi,

i got a string contain :

jessica - 21
jessica - old trafford
jessica - female
jessica - blonde
jessica - merried
markus - 34
markus - canada
markus - male
markus - blonde
markus - single

how to make the output become like this :

jessica - 21,old trafford,female,blonde,merried
markus - 34,canada,male,blonde,single

thank you
abangbataxAsked:
Who is Participating?
 
agamalConnect With a Mentor Commented:
check this


change
echo "\n";
to
echo "<br>";

if you want to see the output on html

<?php
$resarray = array();
$string='jessica - 21
jessica - old trafford
jessica - female
jessica - blonde
jessica - merried
markus - 34
markus - canada
markus - male
markus - blonde
markus - single';
 
$lines = explode("\n",$string);
for ($i=0;$i<count($lines);$i++) {
$pairs = explode("-",$lines[$i]);
$resarray[$pairs[0]][$i] =trim($pairs[1]);
}
foreach($resarray as $key => $value) {
echo $key." - ".implode(",",array_values($resarray[$key]));
echo "\n";
}
?>

Open in new window

0
 
v2MediaCommented:
0
 
Roger BaklundCommented:
Please try to make proper questions in the subject. This goes into an archive, "another question" and "hi again" is not very informative... ;)

This should do it:
$string = "jessica - 21
jessica - old trafford
jessica - female
jessica - blonde
jessica - merried
markus - 34
markus - canada
markus - male
markus - blonde
markus - single";
$lines = explode("\n",$string);
foreach($lines as $line) {
  list($name,$data) = explode(' - ',$line,2);
  if(!isset($result[$name])) $result[$name] = array();
  $result[$name][] = trim($data);
}
foreach($result as $name=>$data)
  echo $name.' - '.implode(',',$data).'<br />';

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
abangbataxAuthor Commented:
how if the splitting not by "\n"

$lines = explode("\n",$string);

but by " - "

$lines = explode(" - ",$string);

how to code to show

jessica - 21,old trafford,female,blonde,merried
markus - 34,canada,male,blonde,single
0
 
Roger BaklundCommented:
That would not work, because then your data contains the "record separator":

jessica - 21 - jessica - old trafford - jessica - female - jessica - blonde

Now it is just a list of words, not pairs.
0
 
agamalCommented:
sorry .... why we will use splitting by - instead of  \n

you have provided a certain format for the string ... so i believe this is the best way to handle this format .....

if you want to change the processing of the string let us see the new format .. so we can advise the appropriate changes to processing steps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.