0

Ok, what i have is a PHP array that looks like this:

Array
(
    [0] => Array
        (
            [label] => 1
            [value] => Value example
        )
    [1] => Array
        (
            [label] => 10
            [value] => Value example 2
        )
    [...]
)

Now, if i json_encode() this array, what i get is:

[
    Object { label="1", value="Value example" },
    Object { label="10", value="Value example 2" },
    ...
]

But to use it in jQuery Autocomplete i need the array to be like this:

[
    { label="1", value="Value example" },
    { label="10", value="Value example 2" },
    ...
]

I've read tons of pages without finding a solution...can someone help?

UPDATE FOR PETER:

Here's my code:

$results = array();
foreach ($temp as $tmp) {
    $results[] = array(
        'label' => $tmp['id'],
        'value' => $tmp['it']
    );
};
echo json_encode($results);

If it may be useful, $temp array is generated from the following Wordpress function:

$wpdb->get_results($query, ARRAY_A);

UPDATE FOR 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%">
6
  • 2
    this is not json_encode output. there are : characters instead = and there is no "Object". proof. Also json_encode is perfectly fine for jquery autocomplete. Also = is not correct JSON. Please show us your code. Commented Nov 19, 2014 at 13:27
  • @Peter i've updated the question for you. Commented Nov 19, 2014 at 13:39
  • it looks ok. can you show me actual result of "json_encode"? I am guessing you copy-pasted data from browser console which is not raw text you send to browser. I think your PHP code is fine, there is some problem on javascript side Commented Nov 19, 2014 at 13:42
  • Yes, i've copy-pasted the code from the console. The actual result taken from the browser output seems to be the correct format that jQuery Autocomplete needs. Then why the autocomplete is not working? [{"label":"1","value":"Value example"},{"label":"10","value":"Value example 2"},...] Commented Nov 19, 2014 at 13:47
  • make sure your selector $('#whatever') is correct. this trivial mistake is often hard to find. also can you show how do you initiate autocomplete in js? Commented Nov 19, 2014 at 13:49

1 Answer 1

1

I just realized what simple mistake you did

Switch label with value:

$results = array();
foreach ($temp as $tmp) {
    $results[] = array(
        'label' => $tmp['it'],
        'value' => $tmp['id']
    );
};
echo json_encode($results);

and it will works

your array should look like this:

Array
(
    [0] => Array
        (
            [label] => Value example
            [value] => 1
        )
    [1] => Array
        (
            [label] => Value example 2
            [value] => 10
        )
    [...]
)
Sign up to request clarification or add additional context in comments.

3 Comments

i tried and there's no difference...why switching them should make any difference?
@Mariano because it gives you hint for label not for value
OMG...what a stupid mistake...now it's working (i've hardly refreshed the browser since the first comment)...thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.