【发布时间】:2014-01-11 11:13:07
【问题描述】:
我通常有这样的数组
(int) 0 => abc,
(int) 1 => def,
(int) 2 => ghi
如果我使用 json_encode 它将变成
["abc", "def", "ghi"]
这对于 jquery 自动完成非常有用
$(function() {
var availableTags = <?php echo json_encode($companyList); ?>;
$("#CompanyName").autocomplete({
source: availableTags,
delay: 10
});
});
但是现在,我需要更多数据,我看一下 jquery 自动完成示例,数据必须看起来像这样
var company = [
{
value: "jquery",
label: "jQuery",
name: "the write less, do more, JavaScript library",
address: "jquery_32x32.png",
city: "xxxxx",
},
{
value: "jquery-ui",
label: "jQuery UI",
name: "the write less, do more, JavaScript library",
address: "jquery_32x32.png",
city: "xxxxx",
},
{
value: "sizzlejs",
label: "Sizzle JS",
name: "the write less, do more, JavaScript library",
address: "jquery_32x32.png",
city: "xxxxx",
}
];
我该如何做 json_encode(或任何其他方式)来更改这样的 PHP 数组
array(
(int) 0 => array(
'Company' => array(
'id' => '19',
'group_id' => '1',
'name' => 'Harts Harts',
'address' => 'xxx NE xxxth Street',
'city' => 'Shoreline',
'state' => 'WA',
'zip' => '98155',
'country' => '',
)
),
(int) 1 => array(
'Company' => array(
'id' => '21',
'group_id' => '1',
'name' => 'Andy Robin',
'address' => 'xxx xxxth Ave NE',
'city' => 'Bellevue',
'state' => 'WA',
'zip' => '98004',
'country' => '',
)
)
)
变成类似于jquery自动完成源的东西,因为如果我直接使用json_encode($company),它会变成对象,我不能用它来自动完成。
这个数组将有大约 2500 个数据 谢谢
【问题讨论】:
标签: php jquery json cakephp-2.0 jquery-autocomplete