由于您不愿意采用简单的方法并修改数据库查询以使用别名:
$result=mysqli_query($link,$qry);
while($obj = mysqli_fetch_object($result))
{
if (isset($obj->id_a)) {
$obj->id = $obj->id_a;
unset($obj->id_a);
} elseif (isset($obj->id_b)) {
$obj->id = $obj->id_b;
unset($obj->id_b);
} elseif (isset($obj->id_c)) {
$obj->id = $obj->id_c;
unset($obj->id_c);
}
$arr[] = $obj;
}
echo '{"results":'.json_encode($arr).'}'
编辑
$objects = array();
$object1 = new stdClass();
$object1->id_a = 1;
$object1->title = "Title 1";
$object2 = new stdClass();
$object2->id_b = 2;
$object2->title = "Title 2";
$objects[0] = $object1;
$objects[1] = $object2;
foreach($objects as $obj) {
if (isset($obj->id_a)) {
$obj->id = $obj->id_a;
unset($obj->id_a);
} elseif (isset($obj->id_b)) {
$obj->id = $obj->id_b;
unset($obj->id_b);
} elseif (isset($obj->id_c)) {
$obj->id = $obj->id_c;
unset($obj->id_c);
}
var_dump($obj);
}
给
object(stdClass)[1]
public 'title' => string 'Title 1' (length=7)
public 'id' => int 1
object(stdClass)[2]
public 'title' => string 'Title 2' (length=7)
public 'id' => int 2