1

出力(print_r)の配列があります:

Array
(
[a] => 706
[b] => 194
[c] => 164
[d] => 44
[e] => 42
[f] => 41
[g] => 40
[h] => 38
[i] => 13
)

これを次の形式の json に変換する必要があります。

[{"name":"a","value":706},{"name":"b","value":194},{"name":"c","value":164},{"name":"d","value":44},{"name":"e","value":42},{"name":"f","value":41},{"name":"g","value":40},{"name":"h","value":38},{"name":"i","value":13}] 

これは私が試したものですが、推測しているだけで、正しい出力が得られません:

 echo  '[';
foreach($sortedArray as $key => $value) {
echo  $dataTable = json_encode(array(
    'name' => $key,
    'value' => $value
), JSON_NUMERIC_CHECK);
};
 echo  ']';
4

3 に答える 3

0
$keyPair = array();
foreach($sortedArray as $key => $value) {
  $keyPair[] = array('name' => $key, 'value' => $value);
}
echo json_encode($keyPair);

http://codepad.org/xApWlo7j

于 2013-08-04T22:26:06.820 に答える
0

これを試して:

$data = array(
  'a' => 234, 'b' => 2343, 'c' => 23423, /* etc. */
);

$new_data = array();
foreach ($data as $k => $v) {
  $new_data[] = array(
    'name' => $k, 'value' => (number)$v
  );
}

print json_encode($new_data);
于 2013-08-04T22:26:14.447 に答える