开发者

add array element to row returned from sql query

I want to add an ad开发者_Python百科ditional value into an array before passing it to json_encode function, but I can't get the syntax right.

 $result = db_query($query);

  // $row is a database query result resource
  while ($row = db_fetch_object($result)) {

  $stack[]  = $row; 
                // I am trying to 'inject' array element here
  $stack[]['x']  = "test";  
 }

echo json_encode($stack); 


If it's an array you can directly add a value:

$row['x'] = 'test';
$stack[] = $row;

if it's an object you can add another property:

$row->x = 'test';
$stack[] = $row;

if you want to keep the object and the extra value separated:

$data = array($row, 'x' => 'test');
$stack[] = $data;


but this does work.

$stack[]  = $row;       
$row->x = 'test';


How about something like:

$row['x'] = 'test';
$stack = $row;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜