S
S
Sergey Valitov2015-07-17 23:56:09
PHP
Sergey Valitov, 2015-07-17 23:56:09

How to group an array in php?

Hello! There is an array like

array (
  2012 => 
  array (
    'year' => '2012',
    'title' => 'Counter-Strike: Global Offensive',
    'link' => 'http://store.steampowered.com/app/730/?snr=1_7_7_230_150_1',
  ),
  2015 => 
  array (
    'year' => '2015',
    'title' => 'The Elder Scrolls® Online: Tamriel Unlimited™',
    'link' => 'http://store.steampowered.com/app/306130/?snr=1_7_7_230_150_1',
  ),
  2013 => 
  array (
    'year' => '2013',
    'title' => 'Marvel Heroes 2015',
    'link' => 'http://store.steampowered.com/app/226320/?snr=1_7_7_230_150_1',
  ),
  2014 => 
  array (
    'year' => '2014',
    'title' => 'Ultra Street Fighter® IV',
    'link' => 'http://store.steampowered.com/app/45760/?snr=1_7_7_230_150_1',
  ),
  2007 => 
  array (
    'year' => '2017',
    'title' => 'Team Fortress 2',
    'link' => 'http://store.steampowered.com/app/440/?snr=1_7_7_230_150_1',
  ),
  2011 => 
  array (
    'year' => '2011',
    'title' => 'Warhammer 40,000: Dawn of War II: Retribution',
    'link' => 'http://store.steampowered.com/app/56437/?snr=1_7_7_230_150_1',
  ),
  2006 => 
  array (
    'year' => '2006',
    'title' => 'Garry\'s Mod',
    'link' => 'http://store.steampowered.com/app/4000/?snr=1_7_7_230_150_1',
  ),
  2009 => 
  array (
    'year' => '2009',
    'title' => 'Warhammer® 40,000™: Dawn of War® II',
    'link' => 'http://store.steampowered.com/app/15620/?snr=1_7_7_230_150_1',
  ),
  2010 => 
  array (
    'year' => '2010',
    'title' => 'Warhammer® 40,000: Dawn of War® II Chaos Rising',
    'link' => 'http://store.steampowered.com/app/20570/?snr=1_7_7_230_150_1',
  ),
)

I need to split the given array into several (by years). To be something like this:
array (
  2012 => array(
           array(123...),
           array(456...),
           array(45466...)
  ),
  2013 => array(
            array(4644...),
           array(5474574...)
  ),
  2014 => array(
            array(4644...),
           array(5474574...)
  )
)

Tobish, so that the YEAR fields are the keys .. so that it is grouped .. I will be very grateful for the help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Petrov, 2015-07-18
@serejatoje

If you don’t bother too much, then you can bust

$array = <исходный массив>;
foreach($array as $key => $val)
{
    $array[$val['year']] = $val;
    unset($array[$key]);
}

UPD: if there are arrays with the same years in the main array and they need to be grouped, then like this:
$array = <исходный массив>;
foreach($array as $key => $val)
{
    $array[$val['year']][] = $val;
    unset($array[$key]);
}

UPD2: it is better to save the result in a new array, otherwise we will lose something
$array = <исходный массив>;
$result = array();
foreach($array as $key => $val)
{
    $result[$val['year']][] = $val;
}

A
Arris, 2015-07-18
@Arris

And what, now he is not broken for years?
What is the problem to use:
$an_array[ $year ] [ 'title' ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question