Jump to content

[SOLVED] This should be easy! But please help


jubbs

Recommended Posts

Hi all,

Sorry to resort to posting yet another "help me" topic, but im really stuck.

This should be so easy, im sure you'll be able to get it *just like that*.

 

eg,

$a = "20070524;apple";
$b = "20070524;banana";
$c = "20070524;carrot";
$d = "20070527;dog";
$e = "20070708;elephant";

 

I am trying to sort the dates from the text, so that similar dates become formatted into a string like:

20070524;apple,banana,carrot

20070527;dog

20070708;elephant

 

Can you please post some code to do this? Whether using an array or string variables, I dont mind. As long as it works, I can get an idea and adapt it to my code.

 

Thanks in advance.

Jubbs

try

<?php
$a = "20070524;apple";
$b = "20070524;banana";
$c = "20070524;carrot";
$d = "20070527;dog";
$e = "20070708;elephant";
$set = array($a, $b, $c, $d, $e);
$out = array();
foreach ($set as $v) {
$a = explode(';', $v);
$out[$a[0]][] = $a[1];
}
foreach ($out as $k => $v) {
$out[$k] = $k.';'.implode(',',$v);
}
foreach ($out as $v) echo $v, "<br />\n";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.