Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Json编码自动完成源代码_Php_Jquery_Json_Cakephp 2.0_Jquery Autocomplete - Fatal编程技术网

Php Json编码自动完成源代码

Php Json编码自动完成源代码,php,jquery,json,cakephp-2.0,jquery-autocomplete,Php,Jquery,Json,Cakephp 2.0,Jquery Autocomplete,我通常有这样的数组 (int) 0 => abc, (int) 1 => def, (int) 2 => ghi var company = [ { value: "jquery", label: "jQuery", name: "the write less, do more, JavaScript library", address: "jquery_32x32.png", city: "xxxxx", }, {

我通常有这样的数组

(int) 0 => abc,
(int) 1 => def,
(int) 2 => ghi
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",
  }
];
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' => '',
        )
    )
)
如果我使用json_编码,它将变成

["abc", "def", "ghi"]
这对于jquery自动完成非常有用

$(function() {
    var availableTags = <?php echo json_encode($companyList); ?>;
    $("#CompanyName").autocomplete({
        source: availableTags,
        delay: 10
    });
}); 
如何进行json_编码(或任何其他方式)来更改这样的PHP数组

(int) 0 => abc,
(int) 1 => def,
(int) 2 => ghi
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",
  }
];
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的autocomplete源代码,因为如果我直接使用json_encode($company),它将成为对象,而我不能将其用于autocomplete

这个阵列将有大约2500个数据
谢谢

使用jQuery parseJSON方法解析从PHP获得的json

var obj = jQuery.parseJSON( 'json_string_here' );
输出:

[{"id":"19","group_id":"1","name":"Harts Harts","address":"xxx NE xxxth Street","city":"Shoreline","state":"WA","zip":"98155","country":""},{"id":"21","group_id":"1","name":"Andy Robin","address":"xxx xxxth Ave NE","city":"Bellevue","state":"WA","zip":"98004","country":""}]

注意:要用于jquery自动完成,您需要在返回的
json
中添加
标签
字段。您当前没有这两个字段。

api jquery ui:

阅读自动完成api后:

$companyList = array();
foreach($companies as $company){
    array_push( $companyList, $company["Company"]["name"]);
}
返回:

["Harts Harts","Andy Robin"]
autocomplete将使用此计划旧数组或对象属性数组
标签
,如下所示:

[{label:"Harts Harts",value:"Harts Harts"},{label:"Andy Robin",value:"Andy Robin"}]

如果你还有什么问题,就问吧

这样一来,我觉得它会因为大量的数据(2500个数据)而导致问题。翻页时速度会很慢loading@Hart我使用了与美国邮政编码相同的代码,一次提取数千个邮政编码,但如果页面上运行了其他内容,则可能是这样。@Harts如果您是从automcompete的数据库提取邮政编码,您可以在此处使用我的示例: