json - need to json_encode a PHP array in a particular way -
ok, have php array looks this:
array ( [0] => array ( [label] => 1 [value] => value example ) [1] => array ( [label] => 10 [value] => value example 2 ) [...] )
now, if json_encode()
array, is:
[ object { label="1", value="value example" }, object { label="10", value="value example 2" }, ... ]
but use in jquery autocomplete need array this:
[ { label="1", value="value example" }, { label="10", value="value example 2" }, ... ]
i've read tons of pages without finding solution...can help?
update peter:
here's code:
$results = array(); foreach ($temp $tmp) { $results[] = array( 'label' => $tmp['id'], 'value' => $tmp['it'] ); }; echo json_encode($results);
if may useful, $temp
array generated following wordpress function:
$wpdb->get_results($query, array_a);
update peter 2
script:
jquery(document).ready(function($){ var temp_array = function(request, response) { $.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>', type: 'post', datatype: 'json', data: { 'action': 'autocomplete_finder', 'data' : request.term, }, success: function(data) { //response(data); console.log(data); } }); }; $('#containing').autocomplete({ source: temp_array, minlength: 3, select: function(event, ui) { console.log('test') } }); });
html:
<input id="containing" style="width: 98%">
i realized simple mistake did
switch label
value
:
$results = array(); foreach ($temp $tmp) { $results[] = array( 'label' => $tmp['it'], 'value' => $tmp['id'] ); }; echo json_encode($results);
and works
your array should this:
array ( [0] => array ( [label] => value example [value] => 1 ) [1] => array ( [label] => value example 2 [value] => 10 ) [...] )
Comments
Post a Comment