php数组到javascript对象
我有这样的案例:php数组到javascript对象,php,javascript,json,Php,Javascript,Json,我有这样的案例: Array ( [A] => Array ( [2013-08-24] => 1 [2013-08-25] => 2 ) [B] => Array ( [2013-08-25] => 4 ) ) 我有json数据: [{ "1377412272": {
Array
(
[A] => Array
(
[2013-08-24] => 1
[2013-08-25] => 2
)
[B] => Array
(
[2013-08-25] => 4
)
)
我有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_编码的结构如下:
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]
]
如何制作?…PHP
$arr = array("id"=>"1");
json_encode($arr);
Javascript+PHP
var json = jQuery.parseJSON(<?=$arr?>);
var id = json.id;
var json=jQuery.parseJSON();
var id=json.id;
PHP
Javascript+PHP
var json = jQuery.parseJSON(<?=$arr?>);
var id = json.id;
var json=jQuery.parseJSON();
var id=json.id;
在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
)
然后,您可以任意处理返回值,包括创建您所寻找的最终对象。在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
)
然后,您可以随意处理返回值,包括创建最终的搜索对象。要获得这样的输出,您应该这样做
$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)
将输出json_encode
<?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>';
?>
要获得这样的输出,您应该做什么
$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)
将输出json_encode
<?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]]]
恐怕您需要手动编写所有代码。 一个(不太简单)的解决方案是:
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]]]
我想这就是你想要的 输出:
[[“日期”,“A”,“B”],[“2013-08-24”,1,0],“2013-08-25”,2,4]
我想这就是你想要的
输出:
[[“日期”,“A”,“B”],[“2013-08-24”,1,0],“2013-08-25”,2,4],
这不起作用。。我只想把数组([A]=>Array([2013-08-24]=>1[2013-08-25]=>2[B]=>Array([2013-08-25]=>4])变成[[“日期”,“A”,“B”],[2013-08-24,1,0],[2013-08-25,2,4]]这不起作用。。我只想让数组([A]=>Array([2013-08-24]=>1[2013-08-25]=>2[B]=>Array([2013-08-25]=>4])成为[[“日期”,“A”,“B”],[2013-08-24,1,0],[2013-08-25,2,4]]供参考,$的数据
参数是可选的,因此,您不需要在那里传递null
。另外,对于这个用例,$get
更可取。谢谢gresso,但这似乎不是我想要的,我已经将数组php转换为json_encode:[code]{“1374050643”:{“2013-08-24”:1,“2013-08-25”:2},“1377346094:{“2013-08-25”:4}[/code],我想让它成为[code][“date A”,“B”],[2013-08-24,1,0],[2013-08-25,2,4][/code]你能帮我吗?只要确保你的php文件中的最后一条语句是echo json_encode,正如我向你展示的那样,你就能让一切正常工作。阅读代码中的注释,这会有所帮助。如果您已经完成了json_编码,那么只需在脚本的最终语句中重复它。仅供参考,$.post
的data
参数是可选的,因此您不需要在那里传递null
。另外,对于这个用例,$get
更可取。谢谢gresso,但这似乎不是我想要的,我已经将数组php转换为json_encode:[code]{“1374050643”:{“2013-08-24”:1,“2013-08-25”:2},“1377346094:{“2013-08-25”:4}[/code],我想让它成为[code][“date A”,“B”],[2013-08-24,1,0],[2013-08-25,2,4][/code]你能帮我吗?只要确保你的php文件中的最后一条语句是echo json_encode,正如我向你展示的那样,你就能让一切正常工作。阅读代码中的注释,这会有所帮助。如果您已经完成了json_编码,那么只需在脚本的最终语句中重复它即可。@gofrendiasgrad太好了,这就是我想要的+1@goFrendiAsgrard太好了,这就是我想要的+1.