dtuqxb3884 2013-08-24 05:39
浏览 27
已采纳

php数组到javascript对象

I have some case like this:

I have json data:

[{
        "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"
        }
    }
]

An then, I have convert to array 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;
            }
        }

Script above have result in json_encode with structure like this:

Array
( 
    [A] => Array
        (
            [2013-08-24] => 1
            [2013-08-25] => 2
        )

    [B] => Array
        (
            [2013-08-25] => 4
        )

)

and finally, I want it to be javascript object

[
  ["date","A","B"],
  [2013-08-24,1,0],
  [2013-08-25,2,4]
]

How to make it?...

  • 写回答

5条回答 默认 最新

  • douyue1998 2013-08-24 06:29
    关注

    I'm afraid you need to code everything manually. One (not too simple) solution is this:

    <?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 code will produce something like this (which I hope is what you want):

    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条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端