如何json编码php中几个对象数组的对象

时间:2014-09-01 04:57:14

标签: php mysql arrays json object

我想生成如下的json:

{
    "success":[{
        "success": "1"
    }],
    "friends": [
        {
            "name": "ali",
            "phone": "934453"
        },
        {
            "name": "reza",
            "phone": "934453"
        }
    ]
}

这是我从mysql表中查询后的代码:

$i=0
while($row=mysql_fetch_array($result)){

    $friend[$i] = array('name' => $row['name'], 'phone' => $row['phone']);
    $i++;
}
$success = 1;
$result = array('success' => $success , 'friends' => $friend);
echo json_encode($result);

但它不能正常工作。

令人惊讶的是,这是我在尝试你的建议后得到的结果! :

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: requestid in E:\wamp\www\android_login_api\include\DB_Functions.php on line <i>149</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>152808</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='E:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>1.0119</td><td bgcolor='#eeeeec' align='right'>189880</td><td bgcolor='#eeeeec'>DB_Functions->doIhaveNewFriends(  )</td><td title='E:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>134</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: data in E:\wamp\www\android_login_api\include\DB_Functions.php on line <i>169</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>152808</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='E:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>1.0119</td><td bgcolor='#eeeeec' align='right'>189880</td><td bgcolor='#eeeeec'>DB_Functions->doIhaveNewFriends(  )</td><td title='E:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>134</td></tr>
</table></font>
null

1 个答案:

答案 0 :(得分:-1)

试试这个

while($row=mysql_fetch_array($result)){
    $friend = new stdClass;
    $friend->name = $row['name'];
    $friend->phone = $row['phone'];
    $friends[] = $friend;
}
$success = array('success'=>1);
$result = array('success' => $success , 'friends' => $friends);
echo json_encode($result);