1

我有这样的情况:

我有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]
]

怎么实现?...

4

5 回答 5

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]]
于 2013-08-24T06:23:45.023 回答
0

PHP

$arr = array("id"=>"1");
json_encode($arr);

Javascript + PHP

var json = jQuery.parseJSON(<?=$arr?>);
var id = json.id;
于 2013-08-24T05:41:28.313 回答
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");    
}

然后,您可以随心所欲地处理返回值,包括创建您要寻找的最终对象。

于 2013-08-24T06:08:45.193 回答
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]]]
于 2013-08-24T06:29:48.277 回答
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]]

于 2013-08-24T06:31:16.337 回答