Hello! I have problem with remove duplicate value from array. Value in my case is ... year and month (2012-07, 2012-5 ...) without DAY Yes, I know that exist function array_unique(), but do not want so... I want eliminate duplicate value in loop. Here is my simple array: Array ( [0] => Array ( [CAST] => 2012-07-22 ) [1] => Array ( [CAST] => 2012-07-23 ) [2] => Array ( [CAST] => 2012-05-04 ) [3] => Array ( [CAST] => 2012-05-15 ) ) PHP: after foreach I get follow result: 2012-07-22 2012-07-23 2012-05-04 2012-05-15 And I wand get this: 2012-07 2012-05 Thank you!
you can just get the y-m value as you loop through and only reprint the header if it doesn't match the original. ie $ym = '0000-00'; foreach($arr as $row) { if ($ym != substr($row['CAST'],0,7) { // do whatever you need to do when the month changes $ym = substr($row['CAST'],0,7); } } PHP: but if you want to have a useful array I'd do this $rebuilt = array(); foreach($arr as $row) { $rebuilt[substr($row['CAST'],0,7)] = $row; } PHP: * untested, may have typos play around with both doing some echos and var_dumps to see how they give different results.
Hey,you can try this. it will work$array = array(1, 2, 2, 3); $array = array_unique($array); // Array is now (1, 2, 3)
Except that the value that needs to be unique isn't in the array. What s/he has 'Y-m-d' and the uniqueness is based on 'Y-m'
step 1: use foreach to truncate all your dates to yyyy-mm format (e.g. making 2012-07-22 to 2012-07) - by using substr($date, 0, 7) for example step 2: use array_unique()