Answer the question
In order to leave comments, you need to log in
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',
),
)
array (
2012 => array(
array(123...),
array(456...),
array(45466...)
),
2013 => array(
array(4644...),
array(5474574...)
),
2014 => array(
array(4644...),
array(5474574...)
)
)
Answer the question
In order to leave comments, you need to log in
If you don’t bother too much, then you can bust
$array = <исходный массив>;
foreach($array as $key => $val)
{
$array[$val['year']] = $val;
unset($array[$key]);
}
$array = <исходный массив>;
foreach($array as $key => $val)
{
$array[$val['year']][] = $val;
unset($array[$key]);
}
$array = <исходный массив>;
$result = array();
foreach($array as $key => $val)
{
$result[$val['year']][] = $val;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question