I hve a question, how I can display last 3 years? i.e I would like to display them in the second format -

2013, 2012, 2011, All(...)

P.S in All I want to display all other years...

Here is code how I get year list now(it displays all years):

if (!empty($year_list))
                        foreach ($year_list as $key => $year)
                            $year = array_unique($year);
                            rsort($year, SORT_NUMERIC);

                            foreach ($year as $g)
                                if ($key != ' ' && $g != '')
                                    $div_date .= "<span id='".$g."' class='filial_r15 ".$key."'>".$g."</span> ";

                    $data['result']['year_list'] = $div_date;

P.P.S I am new in PHP, so someone of you this question may seem very simple... but I stuck there=(

4 Years
Discussion Span
Last Post by diafol

I'm assuming that you have a list of years (some duplicates - judging from the array_unique function) and you want just the last three? I don't get the "all".
You can use the array_slice to get the first three in a sorted list...

If the $year_list has the type of structure...


Then you need to

$year_unique = array_unique($year_list);
$last3years = array_slice($year_unique, 0, 3);

So if you need a comma separated output:

echo implode(", ", $last3years);

I have a feeling though that's not quite what you want.


it's works, but not how I need... or I do something else...
It only displays 3 last years... but I need display 3 last years as numbers, and other years hide behind "All(...)" list...
so is it possible with array_slice only, or I need something else?


Well, if I understand you correctly...

$year_unique = array_unique($year_list);
$last3years = array_slice($year_unique, 0, 3);
$allothers = array_slice($year_unique, 3);

echo 'First Three: ' . implode(", ", $last3years);
echo '<br />Others: All(' . implode(", ", $allothers) . ')';

But I don't really understand what you mean by

'hide behind All(...)'

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.