English 中文(简体)
• 消除PHP中用于多维阵列的 in星指数
原标题:Get rid of the int index in json_encode in PHP for multi-dimensional arrays

这是难以解释的一点问题,因此,你却不明白。 如果你看上下文,就会看到有名的初等人物。

{
    "data":{
        "0":{
            "action_id":"1",
            "date":"2012-04-10 15:07:38",
            "action_type":"1",
            "action_text":"Some one got blamed!"
        },
        "1":{
            "action_id":"2",
            "date":"2012-04-10 16:18:05",
            "action_type":"1",
            "action_text":"Testing multiple items for AJAX"
        },
        "total":2,
        "ajax_message":"Success",
        "ajax_status":"0",
        "success":"true"
    }
}

但是,在申请使用时,它不能处理“0”:......,”1 ,而只是想把它分开处理。

我目前的法典是:

while ($r = mysql_fetch_assoc($q)) {
        $array[] = $r;
    }
json_encode($array);

此时此刻,充其量只是简单而原始的。 但是,我认为,我可能不得不为我写一个json_encode,以便照此印制。

任何帮助都将受到高度赞赏。

注:这是有效形式(由手写):

{
"data": [
{
"action_id": "1",
"date": "2012-04-10 15:07:38",
"action_type": "1",
"action_text": "Some one got blamed!",
"fb_id": "760775384"
},
{
"action_id": "2",
"date": "2012-04-10 16:18:05",
"action_type": "1",
"action_text": "Testing multiple items for AJAX",
"fb_id": "760775384"
}
],
"total": 2,
"ajax_message": "Success",
"ajax_status": "0",
"success": "true"
}
最佳回答

问题在于,贵重数字钥匙(0,1)与点名相同( Total,ajax_message, 等)。 不这样做:

$a = array();
while ($r = mysql_fetch_assoc($q)){
    $a[] = $r;
}
$a[ key ] = value;

Do this:

$a = array();
$a[ rows ] = array();
while ($r = mysql_fetch_assoc($q)){
    $a[ rows ][] = $r;
}
$a[ key ] = value;

如果阵列($a[rows] in this example)中的每一条钥匙均为数字,json_encode(将将其作为[{list},{ similar},{this}]

问题回答

你们需要额外的阵容。

页: 1

array( 
   data  => array(
     0  => array()
     1  => array()
     total  => 2,
     etc  =>  blah 
  )
)

但你要求这样做:

array( 
   data  => array(
    array(),
    array()
  )
   total  => 2,
   etc  =>  blah 
)

正如Cal公司所说,更深的问题是,你在扼杀你重新编码阵列的钥匙方面混合使用数字钥匙。 这在PHP中是罚款的,但对JSON的阵列却不有效,只有物体。

json_encode,将编码阵列,而不将钥匙作为名人阵列,但需要编码,将关键阵列作为目标,使之具有效力。

while ($r = mysql_fetch_assoc($q)) {
    $array[][] = $r;
}
json_encode($array);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签