2017-01-12 22 views
-1

私はこのような単純な二次元配列を持っている:PHPグループ多次元配列

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => John 
      [company] => One 
      [price] => 12.22 
     ) 

    [1] => Array 
     (
      [id] => 1 
      [name] => John 
      [company] => Two 
      [price] => 14.33 
     ) 
    [2] => Array 
     (
      [id] => 2 
      [name] => Mike 
      [company] => One 
      [price] => 15.11 
     ) 
    [3] => Array 
     (
      [id] => 2 
      [name] => Mike 
      [company] => Two 
      [price] => 10.12 
     ) 

    [4] => Array 
     (
      [id] => 3 
      [name] => Paul 
      [company] => One 
      [price] => 42.22 
     ) 
    [5] => Array 
     (
      [id] => 3 
      [name] => Paul 
      [company] => Two 
      [price] => 56.62 
     ) 
    [6] => Array 
     (
      [id] => 3 
      [name] => Paul 
      [company] => Three 
      [price] => 16.12 
     ) 
) 

私はその後、別の値を持つ配列を作成し、グループidnameに必要なこのような何か:

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => John 
      [companies] => array (
            array(
             [company] => One 
             [price] => 12.22 
           ) 
            array(
             [company] => Two 
             [price] => 14.33 
           ) 
          )    
     ) 

    [1] => Array 
     (
      [id] => 2 
      [name] => Mike 
      [companies] => array (
            array(
             [company] => One 
             [price] => 15.11 
           ) 
            array(
             [company] => Two 
             [price] => 10.12 
           ) 
          )    
     ) 
    [2] => Array 
     (
      [id] => 3 
      [name] => Paul 
      [companies] => array (
            array(
             [company] => One 
             [price] => 42.22 
           ) 
            array(
             [company] => Two 
             [price] => 56.62 
           ) 
            array(
             [company] => Three 
             [price] => 16.12 
           ) 
          )    
     )   
) 

PHPで行う最良の方法は何ですか?

これは私のattempです:

<?php 
$items=array(); 
$temp = 0; 
$companies = array('uno','dos','tres'); 
foreach ($values as $value) { 

    if ($temp == $value['id']) 
    continue;        
    else 
    $temp == $value['id']; 

    foreach ($companies as $key => $company){ 
    foreach ($values as $item){ 
     if ($item['id'] == $temp && $item['company'] == $key) 
     $value['company'][$key] = $item['price']; 
    } 
    } 

    $items[] = $value; 

} 
+2

私はそれを行うにはいくつかの方法が考えられます。これまでに何を試しましたか?これはコード作成サービスではありません。 – miken32

+0

私の考え方を追加しました – kurtko

+0

あなたは名前でグループ化したいと言っていますが、あなたのコードには 'name'とは言いません。 – miken32

答えて

1

は、あなたがする必要がある場合は、すぐ隣companyprice

foreach($array as $v) { 
    $result[$v['id']]['id']   = $v['id']; 
    $result[$v['id']]['name']  = $v['name']; 
    $result[$v['id']]['companies'][] = array('company' => $v['company'], 
              'price' => $v['price']); 
} 

で新しい配列を追加し、新しい配列のキーとしてidを使用しますそれを再インデックスする(恐らくそうではない):

$result = array_values($result); 
+0

完璧なソリューション。ありがとう! – kurtko