我有这样的情况:
我有json数据:
[{
"1377412272": {
"user_id": "1374050643",
"date": "2013-08-24",
"ip": "::1"
}
},
{
"1377412279": {
"user_id": "1374050643",
"date": "2013-08-25",
"ip": "::1"
}
}
, {
"1377412287": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
}, {
"1377413058": {
"user_id": "1374050643",
"date": "2013-08-25",
"ip": "::1"
}
},
{
"1377413069": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
}
, {
"1377413074": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
},
{
"1377413079": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
}
]
然后,我已经转换为数组PHP
$newArr = array();
foreach ($view['con'] as $key => $value) {
foreach ($value as $k => $v) {
if (isset($newArr[$v['user_id']][$v['date']])) {
$newArr[$v['user_id']][$v['date']]++;
}
else
$newArr[$v['user_id']][$v['date']] = 1;
$newArr[$v['user_id']][$v['date']] = isset($newArr[$v['user_id']][$v['date']]) ? $newArr[$v['user_id']][$v['date']]++ : 1;
}
}
上面的脚本导致json_encode的结构如下:
Array
(
[A] => Array
(
[2013-08-24] => 1
[2013-08-25] => 2
)
[B] => Array
(
[2013-08-25] => 4
)
)
最后,我希望它是javascript对象
[
["date","A","B"],
[2013-08-24,1,0],
[2013-08-25,2,4]
]
如何制作?...
答案 0 :(得分:1)
要获得这样的输出你应该做
$countArr = array();
foreach ($data as $key => $value)
{
foreach ($value as $k => $v)
{
if (isset($countArr[$v['date']][$v['user_id']]))
{
$countArr[$v['date']][$v['user_id']]++;
}
else
{
$countArr[$v['date']][$v['user_id']] = 1;
}
}
}
$newArr = array();
foreach ($countArr as $date => $val)
{
$row = array($date);
$newArr[] = array_merge(array($date), array_values($val));
}
echo "<pre>";
print_r($newArr);
echo json_encode($newArr)
如果你打印出$ newArr,它将会是这样的
Array
(
[0] => Array
(
[0] => 2013-08-24
[1] => 1
)
[1] => Array
(
[0] => 2013-08-25
[1] => 2
[2] => 4
)
)
json_encode将输出
[["2013-08-24",1],["2013-08-25",2,4]]
答案 1 :(得分:0)
PHP
$arr = array("id"=>"1");
json_encode($arr);
Javascript + PHP
var json = jQuery.parseJSON(<?=$arr?>);
var id = json.id;
答案 2 :(得分:0)
在php中,我们称之为 array.php :
// Your final statment of your array.php script must be an echo statment to send the array to jQuery
$ar = array('item1' => 'value1');
$ret_val['a'] = $ar;
echo json_encode($ret_val);
在jQuery中(例如,在$.post
的回调中,但您也可以使用$.ajax
)
$.post("/path/to/array.php,
null, // not passing any values to array.php
function(data) {
console.log (data.a.item1); // writes 'value1' to the console
// (note that the 'a' in data.a.item1 in jQuery is the same as the 'a' in $ret_val in PHP)
},
"json");
}
然后你可以随意处理返回值,包括创建你想要的最终对象。
答案 3 :(得分:0)
我担心你需要手动编码所有内容。 一个(不太简单)的解决方案就是:
<?php
$ori_list = array(
'A'=> array(
'2013-08-24' => 1,
'2013-08-25' => 2,
),
'B'=> array(
'2013-08-24' => 3,
),
);
$modif_list = array();
// prepare the header
$header = array('date');
foreach($ori_list as $key=>$val){
if(!array_key_exists($key, $header)){
$header[] = $key;
}
}
$modif_list[] = $header;
// prepare the date_list
$date_list = array();
foreach($ori_list as $key=>$val){
foreach($val as $date=>$num){
// add the initial row for every date
$registered = false;
foreach($date_list as $date_row){
if($date_row[0] == $date){
$registered = true;
break;
}
}
if(!$registered){
$date_row = array($date);
for($i=0; $i<count($header)-1; $i++){
$date_row[] = 0;
}
$date_list[] = $date_row;
}
// put the right value to the right row
$first_index = 0;
$second_index = 0;
for($i=1; $i<count($header); $i++){
if($header[$i] == $key){
$second_index = $i;
break;
}
}
for($i=0; $i<count($date_list); $i++){
if($date == $date_list[$i][0]){
$first_index = $i;
break;
}
}
$date_list[$first_index][$second_index] = $num;
}
}
$modif_list[] = $date_list;
echo 'The PHP';
echo '<pre>';
var_dump($modif_list);
echo '</pre>';
echo 'The JSON';
echo '<pre>';
echo json_encode($modif_list);
echo '</pre>';
?>
代码会生成这样的东西(我希望是你想要的):
The PHP
array(2) {
[0]=>
array(3) {
[0]=>
string(4) "date"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
}
[1]=>
array(2) {
[0]=>
array(3) {
[0]=>
string(10) "2013-08-24"
[1]=>
int(1)
[2]=>
int(3)
}
[1]=>
array(3) {
[0]=>
string(10) "2013-08-25"
[1]=>
int(2)
[2]=>
int(0)
}
}
}
The JSON
[["date","A","B"],[["2013-08-24",1,3],["2013-08-25",2,0]]]
答案 4 :(得分:0)
我认为这就是你想要的
$newArray = array
(
"A" => array
(
"2013-08-24" => 1,
"2013-08-25" => 2
),
"B" => array
(
"2013-08-25" => 4
)
);
$uids=array();
$da = array();
foreach($na as $uid => $value)
{
$uids[] = $uid;
foreach($value as $date => $count)
{
$da[$date][$uid]=$count;
}
}
$ra = array(array("date"));
foreach($uids as $uid)
{
$ra[0][] = $uid;
}
$i = 1;
foreach($da as $date => $value)
{
$ra[$i][] = $date;
foreach($uids as $uid)
{
if(array_key_exists($uid,$value))
{
$ra[$i][] = $value[$uid];
}
else
{
$ra[$i][] = 0;
}
}
$i++;
}
print(json_encode($ra));
输出:
[["date","A","B"],["2013-08-24",1,0],["2013-08-25",2,4]]