PDO Array mySQL with Json display error

1

I'm having a hard time json_encode

echo json_encode($autocomplete -> fetchAll(PDO :: FETCH_ASSOC));

I'm doing a select from the database so fine, but when it transfers the data to this echo from above, many characters appear that javascript is not accepting, such as transforming the output from above that is displayed as below

[{"user_name":"Roberto Monteiro"},{"user_name":"Dk Teclive"},{"user_name":"Fye Flourigh"}]

In the format below that is accepted by javascript

["Roberto Monteiro", "Dk Teclive", "Fye Flourigh"];

that is, how can I turn this on?

[{"user_name":"Roberto Monteiro"},{"user_name":"Dk Teclive"},{"user_name":"Fye Flourigh"}]

and leave in this format

["Roberto Monteiro", "Dk Teclive", "Fye Flourigh"];

Whether with php or javascript, if possible, both ways, grateful now.

    
asked by anonymous 03.03.2016 / 17:41

2 answers

1

In your javascript the first step is to parse the string sent by php into a valid json, then you can access the array like this: lista[i].user_name or lista[i]['user_name'] or you can still use jquery to make lista a simple array it is also possible to do with pure javascript).

var lista = JSON.parse('[{"user_name":"Roberto Monteiro"},{"user_name":"Dk Teclive"},{"user_name":"Fye Flourigh"}]');
var array_simples = $.map(lista, function (item) {
        return item.user_name;
    });
    
03.03.2016 / 18:19
0

Maybe using array_values :

echo json_encode($autocomplete -> array_values(fetchAll(PDO :: FETCH_ASSOC)));

    
03.03.2016 / 17:48