简体   繁体   English

PHP数组到JavaScript对象

[英]php array to javascript object

I have some case like this: 我有这样的情况:

I have json data: 我有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"
        }
    }
]

An then, I have convert to array PHP 然后,我已经转换为数组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: 上面的脚本在json_encode中的结果如下:

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 最后,我希望它成为javascript对象

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

How to make it?... 怎么做?...

To get output like this yo should do 为了获得这样的输出哟应该做

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

If you print out $newArr it will look like this 如果您打印$ newArr,它将看起来像这样

Array
(
    [0] => Array
        (
            [0] => 2013-08-24
            [1] => 1
        )

    [1] => Array
        (
            [0] => 2013-08-25
            [1] => 2
            [2] => 4
        )

)

json_encode will output json_encode将输出

[["2013-08-24",1],["2013-08-25",2,4]]

PHP PHP

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

Javascript + PHP Javascript + PHP

var json = jQuery.parseJSON(<?=$arr?>);
var id = json.id;

In php, lets call it array.php : 在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); 

In jQuery (for example, in the callback of $.post , but you can use $.ajax as well) 在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");    
}

Then you deal with the return value anyway you like, including creating the final object you seek. 然后,您可以随意处理返回值,包括创建要查找的最终对象。

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

I think this is what you want 我想这就是你想要的

$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));

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM