如何使用数据记录集格式化数组


How to format an array using a recordset for data

我想用数据库中的数据填充数组。我需要它有不同的标签,不同于在数据库中声明的标题。

这就是我需要格式化的方式

[ { label: "Choice1", value: "value1" }, ... ]

我用过这个,但后来我得到了错误的标签

$items = array();
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
  $items[] = $row_Recordset1;
}

例如。

[{"ID":"2","ARTIST":"!!!"},...]

我需要像这样使用jquery ui插件来自动完成

您需要将数据作为JSON对象返回。。。

$items = array();
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
  $items[] = array(
    'label' => $row_Recordset1['ARTIST'],
    'value' => $row_Recordset1['ID']
  );
}
return json_encode($items);

此外,您应该考虑使用PDO而不是MySQL_*函数,因为它们现在已被弃用。

从数据库中获取assoc数据,将它们放入数组中,并对结果集使用json_encode

您需要在关联数组上使用json_encode方法。