D
D
del9937882016-11-30 01:54:38
PHP
del993788, 2016-11-30 01:54:38

How to remove square brackets in php response?

Hello. Here is a request

$row = json_encode($stmt->fetchAll(PDO::FETCH_OBJ));
echo "$row";

returns me the following response: [{"id":"8","namenomer":"lux"}]
How do I change the query so that the response is like this: {"id":"8","namenomer":"lux "} ?
That is, without square brackets.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Immortal_pony, 2016-11-30
@del993788

$row = json_encode(array_pop($stmt->fetchAll(PDO::FETCH_OBJ)));
/* or */
$row = json_encode($stmt->fetch());

P
Pavel K, 2016-11-30
@PavelK

The brackets appear because the return value of $stmt->fetchAll(PDO::FETCH_OBJ) is an array (more info: php.net/manual/en/pdostatement.fetchall.php).
Accordingly, it is enough, $stmt->fetchAll(PDO::FETCH_OBJ)[0] but this is not entirely true, you need to add a check to see if there is a value at all.
Maybe, depending on the task, you can use fetch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question