Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php编码json?_Php_Json - Fatal编程技术网

使用php编码json?

使用php编码json?,php,json,Php,Json,我想用php编码函数获取json,如下所示 <?php require "../classes/database.php"; $database = new database(); header("content-type: application/json"); $result = $database->get_by_name($_POST['q']); //$_POST['searchValue'] echo '{"results":['; if

我想用php编码函数获取json,如下所示

<?php 
  require "../classes/database.php";

  $database = new database();
  header("content-type: application/json");
  $result = $database->get_by_name($_POST['q']);   //$_POST['searchValue']

  echo '{"results":[';
  if($result)
  {
     $i = 1;
     while($row = mysql_fetch_array($result))
     {
        if(count($row) > 1) 
        {
           echo json_encode(array('id'=>$i, 'name' => $row['name']));
           echo ",";
        }
        else 
        {
           echo json_encode(array('id'=>$i, 'name' => $row['name']));
        }
        $i++; 
     }
  }
  else
  {
     $value = "FALSE";
     echo json_encode(array('id'=>1, 'name' => ""));  // output the json code
  }

  echo "]}";
{"results":[{"id":1,"name":"name1"},{"id":2,"name":"name2"},]}
但是输出json如下所示

<?php 
  require "../classes/database.php";

  $database = new database();
  header("content-type: application/json");
  $result = $database->get_by_name($_POST['q']);   //$_POST['searchValue']

  echo '{"results":[';
  if($result)
  {
     $i = 1;
     while($row = mysql_fetch_array($result))
     {
        if(count($row) > 1) 
        {
           echo json_encode(array('id'=>$i, 'name' => $row['name']));
           echo ",";
        }
        else 
        {
           echo json_encode(array('id'=>$i, 'name' => $row['name']));
        }
        $i++; 
     }
  }
  else
  {
     $value = "FALSE";
     echo json_encode(array('id'=>1, 'name' => ""));  // output the json code
  }

  echo "]}";
{"results":[{"id":1,"name":"name1"},{"id":2,"name":"name2"},]}
当您意识到末尾有逗号时,如果我删除了
echo“,”,我想删除它,这样它就可以是正确的json语法当有多个结果时,json将生成如下结果:[{“id”:1,name:“name1”}{“id”:2,name:“name2”}
,语法也错误

希望每个人都明白我的意思,任何想法都会受到欢迎,只要换句话就行了

echo json_encode(array('id'=>$i, 'name' => $row['name']));
echo ",";
对这些

echo ",";
echo json_encode(array('id'=>$i, 'name' => $row['name']));

将json_编码作为最后一步。纯粹用PHP构建数据结构,然后在最后对该结构进行编码。进行中间编码意味着您基本上是在构建自己的json字符串,这总是很棘手,而且很可能是“坏的”


首先将所有内容构建到一个数组中,然后一次性对整个内容进行编码:

$outputdata = array();
while($row = mysql_fetch_array($result)) {
    $outputdata[] = $row;
}

echo json_encode($outputdata);

如果我是你,我不会对每个数组进行
json\u编码
,而是将数组合并在一起,然后在最后对合并后的数组进行
json\u编码
。下面是使用短数组语法的示例:

$out = [];
while(...) {
  $out[] = [ 'id' => $i, 'name' => $row['name'] ];
}
echo json_encode($out);

这是我带来的一个解决方案

 $i = 1; 
 $array = array(); 
 while($row = mysql_fetch_array($result)) 
 { 
 $array[] = json_encode(array('id'=>$i, 'name' => $row['name'])); 
 $i++; 
 } 
 echo implode(',', $array); // Take output array glue it with the  
这将把json放入一个数组中,然后用胶水(,)将其内爆,输出以下
{“results”:[{“id”:1,“name”:“maadi”},{“id”:2,“name”:“monofiya”}]}
,无需先执行数组,然后将其传递给json_encode()函数

创建自己的函数非常简单

function son($ar) {
    $str = "";
    $str .= "{";
    $id = 0;
    foreach($ar as $a=>$b) {
        $id++;
        $str .= "\"".$a."\":";
        if(!is_numeric($b)) {
            $str .= "\"".$b."\"";
        } else {
            $str .= $b;
        }
        
        if($id < count($ar)) {
            $str .= ",";
        }
    }
    $str .= "}";
    return $str;
}
以及使用

<?php
$o = new mysqli(
    "localhost",
    "root",""
);

if($o->connect_error) {
    echo "DUDE what are you/!";
} else {
    if(isset($_GET["t"])) {
        $t = $_GET["t"];
        $rz = mysqli_query($o,
            "SELECT * FROM game.".$t
        );
        $ar = noom($rz);
        echo son($ar);
    } else {
        echo "B\"H<br>Yo, what's krakin?";
    }
}

然后你以
、[]、[]、[]、[]
结束,然后回到基本相同的船上。然后它又出了问题!现在我知道问题出在哪里了。你不必检查行数是否大于1,你必须检查$I是否大于1!!所以将if(count($row)>1改为if($I>1)这肯定会成功!这将写入[]、[]、[]
<?php
$o = new mysqli(
    "localhost",
    "root",""
);

if($o->connect_error) {
    echo "DUDE what are you/!";
} else {
    if(isset($_GET["t"])) {
        $t = $_GET["t"];
        $rz = mysqli_query($o,
            "SELECT * FROM game.".$t
        );
        $ar = noom($rz);
        echo son($ar);
    } else {
        echo "B\"H<br>Yo, what's krakin?";
    }
}