[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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
0
abangbatax
Asked:
abangbatax
1 Solution
 
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
 
agamalCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now