PHP How to restructure an array? -


i have array i'd restructure. want group items turn. can figure out how extract data array using foreach($arr['history'] $obj) issue populating new array using loop.

currently looks this:

array (  [history] => array (      [id] => 23452435      [legend] => array (           [0] => array (              [player] => me              [turn] => 1              [card] => array (                  [name] => foo              )          )           [1] => array (              [player] => me              [turn] => 1              [card] => array (                  [name] => bar             )          )           [2] => array (              [player] => opponent              [turn] => 1             [card] => array (                 [name] => derp              )          )           [3] => array (              [player] => opponent              [turn] => 2              [card] => array (                  [name] => hoo             )          )      )  )) 

i want following, can't figure out how automatically create , populate structure. array sub-array each turn, containing array me , opponent

array ( [0] => array (     [me] => array (         [0] => foo         [1] => bar     )     [opponent] = array (         [0] => derp     )  ) [1] => array (     [me] => array ()     [opponent] => array (         [0] => hoo     ) )) 

thanks.

edit:

this needed. answers.

$result = []; foreach ($arr['history'] $historyitem) {   foreach ($historyitem['legend'] $list) {     $result[$list['turn']][$list['player']][] = $list['card']['name'];   } } 

try this:

$result = []; foreach ($data['history']['legend'] $list) {   $result[$list['turn']-1][$list['player']][] = $list['card']['name']; } 

fiddle it! http://ideone.com/btkokj