Brad Herman
Brad Herman

Reputation: 10695

PHP Array_Unique Problem

I don't know PHP very well, so please bear with me.

My client has a database with information and one of the fields is ff_date_time in the format "Tue Oct 5 14:43:10 2010". There are a lot of entries in here and I need to display a list of just the days that have entries: ie,

and so on. There may be hundreds of entries on a certain day, so obviously when I pull in dates from the db I need to go through and extract the right data and filter it. Here's what I have so far:

$query = "SELECT ff_date_time FROM booth_submit"; 
$query_result = mysql_query($query);
$datetimes = array();
$dates = array();

while ($row = mysql_fetch_array($query_result)) {
  $datetimes[] = $row['ff_date_time'];
}

for ($i = 0; $i < sizeOf($datetimes); $i++) {
  $temp = explode(" ", $datetimes[$i]);
  $dates[] = ($temp[0]." ".$temp[1]." ".$temp[2]." ".$temp[4]);  # Breaks date_time into 'Mon Oct 5 2010' format
}

$dates = array_unique($dates);                      
for ($i = 0; $i < sizeOf($dates); $i++) {
  echo('<a href="#">'.$dates[$i].'</a><br />'); 
}

I'm doing a similar thing for two others fields that work fine, but for some reason, this always yields a $dates array that is the right length (ie: 4 unique dates, array is size 4), but only the first $dates element has any info. The output looks like this:

<a href="#">Mon Oct 3 2010</a><br />
<a href="#"></a><br />
<a href="#"></a><br />
<a href="#"></a><br />

When I don't use array_unique and just test values to check if everything is getting loaded and parsed correctly, the array is just as it should be ("Mon Oct 3 2010","Mon Oct 3 2010","Mon Oct 3 2010","Mon Oct 4 2010","Mon Oct 5 2010","Mon Oct 5 2010","Mon Oct 6 2010").

Any clues what's going wrong here?

Upvotes: 2

Views: 1814

Answers (5)

slosd
slosd

Reputation: 3484

What is the output of var_dump($dates) after you used array_unique?

Try foreach instead of a for loop:

foreach($dates as $date) {
  echo('<a href="#">'.$date.'</a><br />'); 
}

Upvotes: 1

Tesserex
Tesserex

Reputation: 17314

How about instead of treating the symptom, you remove the cause.

Add GROUP BY ff_date_time to the end of your query, or wrap DISTINCT() around ff_date_time in the select. Then you'll only get unique dates in your sql result.

edit: ok, assuming that ff_date_time is actually a datetime field, then in order to ignore time of day, make that GROUP BY DATE(ff_date_time) which will use only the day part and not the time part. If it's not a datetime field, and just a string, you will have to do substring functions, or use someone else's answer.

Upvotes: 1

Ruel
Ruel

Reputation: 15780

Assign the unique array to another array variable. And use foreach.

$newarr = array();
$newarr = array_unique($dates);
foreach ($newarr as $date) {
  echo('<a href="#">'.$date.'</a><br />' . "\n"); 
}

Upvotes: 1

mhitza
mhitza

Reputation: 5715

I would rather recommend you another approach. Using the built-in datetime functions.

$all_dates = array();
foreach($datetimes as $date) {
  $all_dates[] = date('D M j Y', strtotime($date));
}

$unique = array_unique($all_dates);

Upvotes: 1

Colin Fine
Colin Fine

Reputation: 3364

array_unique preserves the keys so your resulting array has elements [0], [4] and [6].

I suggest using

foreach  ($dates as $date) {
  echo('<a href="#">'.$date.'</a><br />'); 
}

Upvotes: 2

Related Questions