简体   繁体   中英

Array format for autocomplete JQuery with JSON

I am trying to change source data of my autocomplete JQuery but i can't understand what kind of array i have to send in PHP at my JS function because i use this to change source data :

$( "#autocomplete" ).autocomplete('option', 'source', tab);

and i send this result to my JS function :

{"transport":{"voiture":"voiture","car":"car","avion":"avion"}}

my php for this result :

foreach ($result as $k => $v)  {        
    //Stockage des valeurs dans un tableau associatif 
        $listtransport[$v['transport']] = $v['transport'];
    }

Thanks in advance !

This is the JSON accepted by the JqueryUI Autocomplete

[
    { 
      id:    elementId, 
      label: label,
      value: value to set when the element is selected
    },
    //Write other elements
]

You could build this array in the follow manner

$ayValues = array();

foreach ($result as $k => $v) {
    $ayValues[] = array(
        'id' => $v['transport']['id index'],         //This is the id of the value
        'label' => $v['transport']['label index'],   //This is the label show
        'value' => $v['transport']['value index']    //This is the value setted
    );
}

print json_encode($ayValues);

Without the content of $result it's impossible to write a clear response.

You could omit the id key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM