2017-03-20 6 views
0

私はいくつかのデータセットを持って、それは重複をグループ化し、マッピングされている必要があり、このように:私が持っているPHPデータマッピング

<?php 
$var = array(
    array("name" => "Alfred","Number" => array(1,2,3)), 
    array("name" => "Aelthestan","Number" => array(4,5,6)), 
) 
?> 

<?php 
$var = array(
    array("Name" => "Alfred", "Number" => 1), 
    array("Name" => "Alfred", "Number" => 2), 
    array("Name" => "Alfred", "Number" => 3), 
    array("Name" => "Aethelstan", "Number" => 4), 
    array("Name" => "Aethelstan", "Number" => 5), 
    array("Name" => "Aethelstan", "Number" => 6), 
) 
?> 

私はそれを変更したいです

誰かがループや1つずつの比較をしていないアイデアはありますか?

+1

名は有効な識別子であれば、あなたは名前が鍵である配列、および数字値の配列を作成することができます。そうでない場合は、ハッシュ関数を使用してキーとして使用できます。 (そして配列にも名前を格納します)。 – Dan

答えて

1

これがうまくいきます。

PHP code demo

<?php 
$var = array(
    array("Name" => "Alfred", "Number" => 1), 
    array("Name" => "Alfred", "Number" => 2), 
    array("Name" => "Alfred", "Number" => 3), 
    array("Name" => "Aethelstan", "Number" => 4), 
    array("Name" => "Aethelstan", "Number" => 5), 
    array("Name" => "Aethelstan", "Number" => 6), 
); 
$result=array(); 
foreach($var as $value) 
{ 
    if(!isset($result[$value["Name"]])) 
    { 
     $result[$value["Name"]]=array("Name"=>$value["Name"],"Number"=>array($value["Number"])); 
    } 
    else 
    { 
     $result[$value["Name"]]["Number"][]=$value["Number"]; 
    } 

} 
$result=array_values($result); 
print_r($result); 

出力:

Array 
(
    [0] => Array 
     (
      [Name] => Alfred 
      [Number] => Array 
       (
        [0] => 1 
        [1] => 2 
        [2] => 3 
       ) 

     ) 

    [1] => Array 
     (
      [Name] => Aethelstan 
      [Number] => Array 
       (
        [0] => 4 
        [1] => 5 
        [2] => 6 
       ) 

     ) 

) 
-1

が可能解決策になることができます:

<?php 
$var = array(
    array("Name" => "Alfred", "Number" => 1), 
    array("Name" => "Alfred", "Number" => 2), 
    array("Name" => "Alfred", "Number" => 3), 
    array("Name" => "Aethelstan", "Number" => 4), 
    array("Name" => "Aethelstan", "Number" => 5), 
    array("Name" => "Aethelstan", "Number" => 6), 
); 

$result = []; 
foreach ($var as $key => $value) { 
    if (!isset($result[$value['Name']])) { 
     $result[$value['Name']] =[]; 
    } 
    $result[$value['Name']][] = $value['Number']; 
} 

$final_result = []; 
foreach ($result as $key => $value) { 
    $final_result[] = ["name" => $key, "number" => $value]; 
} 

print_r($final_result); 
1

あなたはまた、より多くの機能/宣言型のためarray_reduceを見て取ることができます/不変のアプローチ:

<?php 

$flatData = [ 
    ['name' => 'foo', 'number' => 1], 
    ['name' => 'foo', 'number' => 2], 
    ['name' => 'foo', 'number' => 3], 
    ['name' => 'bar', 'number' => 4], 
    ['name' => 'bar', 'number' => 5], 
    ['name' => 'bar', 'number' => 6], 
]; 

$treeData = array_reduce($flatData, function ($tree, $data) { 
    $key = $data['name']; 

    if (empty($tree[$key])) { 
    $tree[$key] = $data; 
    unset($tree[$key]['number']); 
    } 

    $tree[$key]['numbers'][] = $data['number']; 

    return $tree; 
}, []); 

print_r($treeData); 

https://repl.it/G4Cg

関連する問題