json_encode() if the number of elements in the array more then 3

时间:2015-07-31 20:41:07

标签: php arrays json

I have an array that contains the following elements:

array(4) {
  [0]=> array(2) { ["home"]=> string(5) "Niort" ["away"]=> string(12) "Valenciennes" } 
  [1]=> array(2) { ["home"]=> string(15) "Kuban Krasnodar" ["away"]=> string(3) "Ufa" } 
  [2]=> array(2) { ["home"]=> string(17) "Fratangelo, Bjorn" ["away"]=> string(13) "Bhambri, Yuki" } 
  [3]=> array(2) { ["home"]=> string(13) "VfL Wolfsburg" ["away"]=> string(15) "Bayern Mnchen" } 
}

And I am trying to convert it to a JSON array with:

while ($row = $result->fetch_assoc()) {
  array_push($events, $row);
}
echo json_encode($events);

It does not work.
However, when I reduce the number of elements to 3 or less, it works perfectly.

Why is this happening? How can I convert more items?

1 个答案:

答案 0 :(得分:0)

Your php array and code should look like this:

<?php

$array1= array(
array( "home"=>  "Niort" ,"away"=>  "Valenciennes" ), 
array( "home"=>  "Kuban Krasnodar", "away"=>  "Ufa" ), 
array( "home"=>  "Fratangelo, Bjorn", "away"=>  "Bhambri, Yuki" ), 
array( "home"=>  "VfL Wolfsburg", "away"=>  "Bayern Mnchen" ) 
);

echo json_encode($array1);