0

I try to save the rows of a table in an array but I am getting an empty array.
My code is:

$a = array();
$b = array();
if ($result->num_rows > 0){
    while ($row = $result->fetch_assoc()) {
        $b["id"] = $row["id"];
        $b["title"] = $row["title"];
        $b["description"] = $row["description"];
        $b["lat"] = $row["lat"];
        $b["lng"] = $row["lng"];
        $b["walkId"] = $row["walkId"];
        echo json_encode($b);
        array_push($a,$b);
    }
    echo json_encode($a);
}

Using echo json_encode($b); I see the results one by one but the array a is empty.

EDIT 1

After Random's help, using the echo print_r($a,true); I am getting the below screenshot: error

And the screenshot from other tables in which I get the data correctly is: correct

EDIT 2

problem with json_encode. I get the below [{"id":"55","title":"\u00ce\u203a\u00ce\u00bf\u00cf\u2026\u00ce\u00bb\u00ce\u00bf\u00cf\u2026\u00ce\u00b4\u00ce\u00ac\u00ce\u00b4\u00ce\u00b9\u00ce\u00ba\u00ce\u00b1","description":"\u00ce\u201c\u00ce\u00b5\u00ce\u00bd\u00ce\u00b9\u00ce\u00ba\u00ce\u00ae \u00ce\u00b1\u00ce\u00bd\u00ce\u00b1\u00cf\u2020\u00ce\u00bf\u00cf\u0081\u00ce\u00ac \u00cf\u0192\u00cf\u201e\u00ce\u00b1 x\u00ce\u00b1\u00cf\u0081\u00ce\u00b1\u00ce\u00ba\u00cf\u201e\u00ce\u00b7\u00cf\u0081\u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00b9\u00ce\u00ba\u00ce\u00ac \u00cf\u201e\u00ce\u00b7\u00cf\u201a \u00ce\u00b3\u00ce\u00b1\u00cf\u0192\u00cf\u201e\u00cf\u0081\u00ce\u00bf\u00ce\u00bd\u00ce\u00bf\u00ce\u00bc\u00ce\u00af\u00ce\u00b1\u00cf\u201a \u00cf\u201e\u00ce\u00b7\u00cf","lat":"40.634356","lng":"22.940716","walkId":"92"},{"id":"56","title":"\u00ce\u2018\u00ce\u00b3\u00ce\u00bf\u00cf\u0081\u00ce\u00ac \u00ce\u0153\u00ce\u00bf\u00ce\u00b4\u00ce\u00b9\u00ce\u00ac\u00ce\u00bd\u00ce\u00bf","description":"H \u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00bf\u00cf\u0081\u00ce\u00af\u00ce\u00b1 \u00ce\u00bc\u00ce\u00b9\u00ce\u00b1\u00cf\u201a \u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00bf\u00cf\u0081\u00ce\u00b9\u00ce\u00ba\u00ce\u00ae\u00cf\u201a \u00ce\u00b1\u00ce\u00b3\u00ce\u00bf\u00cf\u0081\u00ce\u00ac\u00cf\u201a. \u00cf\u201e\u00ce\u00b1 X\u00ce\u00b1\u00cf\u0081\u00ce\u00b1\u00ce\u00ba\u00cf\u201e\u00ce\u00b7\u00cf\u0081\u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00b9\u00ce\u00ba\u00ce\u00ac \u00cf\u201e\u00ce\u00b7\u00cf\u201a \u00ce","lat":"40.634609","lng":"22.941081","walkId":"92"}

17
  • Are you sure it is getting inside the loop? Commented May 21, 2015 at 8:07
  • Does $b actually contain any data? Commented May 21, 2015 at 8:07
  • 5
    why make it hard for yourself, just use $a[] = $row; Commented May 21, 2015 at 8:08
  • Even if this worked, you'd only get the last value returned by fetch_assoc() because you're overwriting values all the time. Why not just push $row into $a ?? Commented May 21, 2015 at 8:08
  • 1
    this code works... it may come from somewhere else ? sandbox.onlinephpfunctions.com/code/… Commented May 21, 2015 at 8:18

1 Answer 1

0

First thing I note is the special ? that appears 3 time. The problem must come from here.
Try to edit the 3 descriptions that contain the special ? so it doesn't appear again in the print_r.
That must turn array_push into an error.
You could also try to encode it in latin to make it safe. (please follow @deceze link to understand what to do)

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

6 Comments

utf8_encode converts from Latin-1 to UTF-8. The characters shown in the screenshot are clearly not Latin-1 encodable characters, so utf8_encode won't do any good here.
@deceze What should be used in this case so ? ( I'm french, so I always use utf8 to solve problems :) )
@deceze I don't believe this is a duplicated, since the main question is about array_push problem... There were unexpected characters in the DB... The duplicate is for the 2nd question, which is actually about encoding...
There is no actual problem with array_push here, it's all just a problem with encodings.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.