1

I'm having a small issue with my code. What I want to do is show the results of a select as json. This is how I do it:

foreach ($query as $row) {
    $test = json_encode($row);
}

But it returns that:

{0: "34.8", 1: "1441098255", 2: "1", col1: "34.8", col2: "1441098255", col3: "1"}

And I want it like this:

{0: "34.8", 1: "1441098255", 2: "1"}

Or this:

{col1: "34.8", col2: "1441098255", col3: "1"}

I tried doing what they say here, but to no avail.

Edit: The replies are helpful, and they do what I want. The problem now is that I have some sneaky part that for some reason can't seem to get rid of. Here's the resulting json string:

{"col1":"34.8","0":"34.8","col2":"1441098255","col3":"1"}

That "0":"34.8" seems to not want to leave, and I have absolutely no idea where it comes from, and if I change the column order, it always duplicates whatever column comes first. If I put "col3" as the first one, it displays this:

{"col3":"1","0":"1","col2":"1441098255","col1":"34.8"}

Edit2: Turns out the solution was much simpler than what I tried. Here's how to display it properly:

while ($arr = $query->fetch(PDO::FETCH_ASSOC)) {
    echo json_encode($arr);
}
7
  • 1
    you can easily build the right format in your loop for the desired output then after the loop output the array in json Commented Apr 13, 2017 at 10:55
  • @Gert Do you mind explaining how to do that? Commented Apr 13, 2017 at 10:57
  • 1
    It looks like you've got a database result indexed both by numeric IDs and column names - you should be able to specify which you want when fetching the result (in the call to PDOStatement::fetch or similar). You haven't included your database code though, so it's impossible to say exactly what you'd need to change. Commented Apr 13, 2017 at 10:59
  • @iainn you mean the select? I use $query = $this->dbh->prepare(select stuff...) followed by $query->execute(); Commented Apr 13, 2017 at 11:04
  • what is the json data being used for example mobile app to connect to DB ? @Newwt Commented Apr 13, 2017 at 11:06

3 Answers 3

2

Use array_filter with ARRAY_FILTER_USE_KEY flag

$keys = ["col1", "col2", "col3"];
foreach ($query as $row) {
    $filtered = array_filter((Array) $row, function($key) use($keys) {
        return in_array($key, $keys);
    }, ARRAY_FILTER_USE_KEY);
    $test = json_encode($filtered);
}

Notice: If your $row is array not object you don't need to cast it to array (Array) $row

Sign up to request clarification or add additional context in comments.

8 Comments

I get Call to undefined function array_fiter()
@Newwt its literal mistake should be filter
mother of god, and I didn't even notice it haha! It does exactly what I wanted it to do, thank you!
@Newwt i guess from different place because $query = [["col1"=>"x",1=>"y"]]; returns filtered
to be honest you should follow @iainn idea, how comes you get both numeric and associative keys in array?
|
0

You can use array_chunk to get the first three element of $row.

json_encode(array_chunk($row, 3, true)[0]);

2 Comments

why not array_slice($row, 0, 3)
This returns something weird with the json: {"col1":"34.8","0":"34.8","col2":"1441098255"}. Edit: I just noticed it does the same with the answer I marked as "solved". hm.
0
 $data=array();
foreach ($query as $row) {

   $data[]=$row;
   //$test = json_encode($row);
}

$test = json_encode( $data);

1 Comment

Consider providing an explanation for you answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.