Hello!
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=(

Recommended Answers

All 3 Replies

Member Avatar for 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...

array(2013,2011,2001,2012,2013,2010,2000,2012)

Then you need to

$year_unique = array_unique($year_list);
rsort($year_unique);
$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?

Member Avatar for diafol

Well, if I understand you correctly...

$year_unique = array_unique($year_list);
rsort($year_unique);
$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(...)'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.