0

Highchartsがデータのマルチシリーズチャートを生成するために使用する形式でデータ配列を取得しようとしています。

私のデータ配列は次のとおりです。

Array ( 
[answer1] => Array ( [SubQuestion 1] => 3 [SubQuestion 2] => 1 ) 
[answer2] => Array ( [SubQuestion 1] => 2 [SubQuestion 2] => 2 ) 
[answer3] => Array ( [SubQuestion 1] => 1 [SubQuestion 2] => 1 ) 
[answer4] => Array ( [SubQuestion 1] => 1 [SubQuestion 2] => 2 ) 
[answer5] => Array ( [SubQuestion 1] => 0 [SubQuestion 2] => 1 )
)

Highchartsでは次の形式にする必要があります。

$chartdata = array(
array("name" =>"SubQuestion 1","data"=>  array(3,2,1,1,0) ),
array("name" =>"SubQuestion 2","data"=>  array(1,2,1,2,1) )
);

誰かが私の配列を反復処理してHighCharts形式で新しい配列を作成する方法について正しい方向に私を向けることができますか?

すべての助けをありがとう。ループを閉じて、少し変更したバージョンの正解を投稿したかっただけです。$ chartdata = array(); foreach($ series as $ key1 => $ value1){$ i = 0; foreach($ value1 as $ key2 => $ value2){$ chartdata [$ i] ['name'] = $ key2; $ chartdata [$ i] ['data'] [] = $ value2; $ i ++; }}

4

1 に答える 1

1
$myarray = Array ( 
'answer1' => Array ( 'SubQuestion 1'=> 3 , 'SubQuestion 2' => 1 ) ,
'answer2'=> Array ( 'SubQuestion 1' => 2, 'SubQuestion 2' => 2 ) ,
'answer3' => Array ( 'SubQuestion 1' => 1, 'SubQuestion 2' => 1 ) ,
'answer4' => Array ( 'SubQuestion 1' => 1, 'SubQuestion 2' => 2 ) ,
'answer5' => Array ( 'SubQuestion 1' => 0, 'SubQuestion 2' => 1 )
);


$temp = array();
$chartdata = array();
foreach($myarray as $key1 => $value1){
  foreach($value1 as $key2 => $value2){
   if(!in_array($key2, $temp)){
    $temp[] = $key2;
   }
   $chartdata[array_search($key2, $temp)]['name'] = $key2;
   $chartdata[array_search($key2, $temp)]['data'][] = $value2;
  }
}
echo '<pre>';
print_r($chartdata);
于 2013-02-18T16:29:59.490 に答える