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

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.