Json编码自动完成源

I usually have array like this

(int) 0 => abc,
(int) 1 => def,
(int) 2 => ghi

which if I use json_encode it will become

["abc", "def", "ghi"]

this will perfectly be useful for jquery autocomplete

$(function() {
    var availableTags = <?php echo json_encode($companyList); ?>;
    $("#CompanyName").autocomplete({
        source: availableTags,
        delay: 10
    });
}); 

But now, I need more data and I take a look at jquery autocomplete example, the data has to look like this

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",
  }
];

how can I do json_encode (or any other way) to change a PHP array like this

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' => '',
        )
    )
)

into something looks like the jquery autocomplete source, because if I directly use json_encode($company), it will become object and I can't use it for autocomplete.

This array will have around 2500ish data Thank you

$return_arr = array();

$company_arr = array(
    (int) 0 =>  array(
        'id' => '19',
        'group_id' => '1',
        'name' => 'Harts Harts',
        'address' => 'xxx NE xxxth Street',
        'city' => 'Shoreline',
        'state' => 'WA',
        'zip' => '98155',
        'country' => '',
    ),
    (int) 1 => array(
        'id' => '21',
        'group_id' => '1',
        'name' => 'Andy Robin',
        'address' => 'xxx xxxth Ave NE',
        'city' => 'Bellevue',
        'state' => 'WA',
        'zip' => '98004',
        'country' => '',
    )
);

foreach ($company_arr as &$company) {
    array_push($return_arr,$company);
}


echo json_encode($return_arr);

Output:

[{"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":""}]

http://codepad.org/dQ8r52Eq

Note: to use for jquery autocomplete, you will need to add a value or label field in your returned json. You currently don't have neither of those fields.

Use jQuery parseJSON method to parse the json you get from PHP.

var obj = jQuery.parseJSON( 'json_string_here' );

api jquery-ui: autocomplete

After reading the api for autocomplete:

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

returns:

["Harts Harts","Andy Robin"]

autocomplete will work with this plan old array or a array of object properties label and value like so:

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

If you have any more questions just ask.