0

I have a problem with array in php at json_encode.

I have 2 arrays that work in PHP but only $ testArray works when converting to json_encode.

When output in array in php everything works as it should. But when converting to json, only one works.

Output is the same for both.

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    if($row["Autor"] !== "" && $row["Nazev"] !== "")
    {
    $knih_data = [
    "Autor" => "".$row["Autor"]."",
    "Nazev" => "".$row["Nazev"]."",
    "Poznamka" => "".$row["Poznamka"]."",    
    ];
    $knihovna[] = $knih_data;    
    }
}
}





$testArray = [
    [
      "Autor"   => "Autor",
      "Nazev" => " Testuju"
    ],
    [
      "Autor"   => "Autor 2",
      "Nazev" => "Testuju..2",
     "Poznamka" => "Text.. 2"
    ],
    [
      "Autor"   => "Autor 3",
      "Nazev" => "Testuju 3",
      "Poznamka" => "Text.. 3"
    ]
  ];

print_r($testArray); //Work
print_r($knihovna); //Work



  $json_convert = json_encode($testArray);//Work
  echo $json_convert;

  $json_convert = array_values($knihovna);//Notwork
  echo $json_convert;

Output

  Array//testArray
(

[0] => Array
    (
        [Autor] => Někdo
        [Nazev] =>  Zlep�ov�n� podnikov�ch proces
    )

[1] => Array
    (
        [Autor] => Někdo 2
        [Nazev] => Testuju..2
        [Poznamka] => Text.. 2
    )

[2] => Array
    (
        [Autor] => Někdo 3
        [Nazev] => Testuju 3
        [Poznamka] => Text.. 3
    )

)

Array //Knihovna
(

[0] => Array
    (
        [Autor] => Jan Urban
        [Nazev] => 10 nejdra���ch mana�ersk�ch ch
        [Poznamka] => 
    )

[1] => Array
    (
        [Autor] => Forbes
        [Nazev] => ?�slo 3/2013
        [Poznamka] => 
    )

[2] => Array
    (
        [Autor] => Jaroslav Charv�t
        [Nazev] => Firemn� strategie pro praxi
        [Poznamka] => 
    )

[3] => Array
    (
        [Autor] => Nierenberg, Calero, Grayson
        [Nazev] => How to read a person like a bo
        [Poznamka] => 
    )

[4] => Array
    (
        [Autor] => Jim Collins, Jerry I. Porras
        [Nazev] => Jak vybodovat trvale �sp?�nou 
        [Poznamka] => 
    )

[5] => Array
    (
        [Autor] => Miller, Wrobleski, Villafuerte
        [Nazev] => Kultura kaizen
        [Poznamka] => 
    )

[6] => Array
    (
        [Autor] => Simon Synek
        [Nazev] => L�d?i jed� posledn�
        [Poznamka] => 
    )

[7] => Array
    (
        [Autor] => Dan Roam
        [Nazev] => N�pady na ubrousku: ?e�te prob
        [Poznamka] => 
    )

[8] => Array
    (
        [Autor] => Chris Voss, Tahl Raz
        [Nazev] => Nikdy ned?lej kompromis
        [Poznamka] => 
    )

[9] => Array
    (
        [Autor] => Keith Ferrazzi
        [Nazev] => Nikdy nejez s�m
        [Poznamka] => 
    )

[10] => Array
    (
        [Autor] => Simon Synek
        [Nazev] => Objevte sv� pro?
        [Poznamka] => 
    )

[11] => Array
    (
        [Autor] => Michal Martoch 
        [Nazev] => ?�zen� vzd�len�ch pracovn�k?
        [Poznamka] => 
    )

[12] => Array
    (
        [Autor] => Roman Baj?an
        [Nazev] => Techniky public relations aneb
        [Poznamka] => 
    )

[13] => Array
    (
        [Autor] => Alena Svozilov�
        [Nazev] => Zlep�ov�n� podnikov�ch proces?
        [Poznamka] => 
    )

)

There is all ouput in array with print_R

9
  • 1
    If you print $knihovna what do you have as output? Maybe your condition never evaluates to true because of there any results our your query is wrong. Commented Mar 19, 2019 at 22:01
  • Output is the same in both arrays. Commented Mar 19, 2019 at 22:04
  • 2
    You're doing a array_values($knihovna) instead of json_encode. Commented Mar 19, 2019 at 22:05
  • @TheGridCoder, please do var_dump($arrayhere) of two arrays and put it to your post Commented Mar 19, 2019 at 22:05
  • I am thinking this might have to do with the double sets of quotes: --> "Autor" => "".$row["Autor"]."" -- What happens if you set a variable IE $autor = $row["Autor"] And then use that variable in double quotes like so: "Autor" => "$autor", ?? Commented Mar 19, 2019 at 22:08

1 Answer 1

2

Looks like encoding error. To make sure, you can check what's wrong by printing json_last_error() after json_encode()

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

Comments

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.