Php Ajax返回数组中的Json,转换为字符串

Php Ajax返回数组中的Json,转换为字符串,php,json,ajax,Php,Json,Ajax,您好,我有代码Ajax从数据库返回值: function showResult(str) { action = 'http://localhost/mateusz/mateusz/public/villages/query/'; if (str.length==0) { document.getElementById("resultSearch").innerHTML=""; document.getElementById("resultSearch").classL

您好,我有代码Ajax从数据库返回值:

function showResult(str) {
    action = 'http://localhost/mateusz/mateusz/public/villages/query/';
  if (str.length==0) {
    document.getElementById("resultSearch").innerHTML="";
    document.getElementById("resultSearch").classList.remove("border-search-city");
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("resultSearch").innerHTML=this.responseText;
      document.getElementById("resultSearch").classList.add("border-search-city");
    }
  }
  xmlhttp.open("GET",action + str, true);
  xmlhttp.send();
}
并编写php代码以连接数据库:

public function query($name) {
    $villages = Village::select('id','name', 'province_id')->where('name', 'like', '%' . $name . '%')->get();

    for ($i=0; $i<=count($villages); $i++) {
      $returnQuery = $villages;
    }

    if ($returnQuery != '' || $returnQuery != null) {
      return '<a href="">'.$returnQuery.'</a>';
    } else {
      return "Nic nie mam";
    }
}
但当我搜索名称“Dobre”时,只返回一条记录,但在数据库中,我有3条名为“Dobre”的记录。方法
print\u r()
返回:

数组([0]=>Array([id]=>261[name]=>Dobre[province\u id]=>2 )=>数组([id]=>1128[name]=>Dobre[province\u id]=>7)[2] =>数组([id]=>1992[name]=>Dobre Miasto[province\u id]=>14))

我不知道如何设置此行的数组数
[0]

echo $someArray[0]["name"];

首先将json转换为数组。使用了
json\u decode()


用于PHP

您可以使用
json\u decode()


我找到了解决方案,只需将循环更改为
foreach

    foreach ($villages as $key => $value) {
      echo "<a href='".$value["id"]."'>".$value["name"]."</a><br>";
    }
foreach($key=>$value){
回声“
”; }
帮助我创建此链接:从。

基于您的文本 但当我搜索名称“Dobre”时,只返回一条记录,但在数据库中,我有3条记录的名称为“Dobre”。方法print\r()返回:

您的问题与json无关,但它只返回一条记录而不是第三条记录,对吗

您需要遍历记录数据

也许是这样的

public function query($name) {
    $villages = Village::select('id','name', 'province_id')->where('name', 'like', '%' . $name . '%')->get();
    $returnQuery = '';

    try {
        $villages = json_decode($villages, true);
        for ($i=0; $i < count($villages); $i++) {
          $returnQuery = '<a href="">' . $villages[$i]['name'] . '</a>';
        }
    } catch (Exception $e) {
        // Always good idea to catch json to prevent error if bad data is pass.
    }

    if ($returnQuery != '') {
      return $returnQuery;
    } else {
      return "Nic nie mam";
    }
}
公共函数查询($name){
$villages=Village::select('id','name','province_id')->where('name','like','%'。$name.'%')->get();
$returnQuery='';
试一试{
$villages=json_decode($villages,true);
对于($i=0;$i
您是否尝试过json_decode()方法。谢谢,但我需要在phpNot中转换json在colnsole中工作我有错误:500(内部服务器错误)谢谢您的回答,但仍然不是正确的工作,因为有相同的地名。当我使用
json\u decode()
谢谢,我已经解决了这个问题;)时,我编辑了我的帖子
// Convert JSON string to Array
  $someArray = json_decode($someJSON, true);
  print_r($someArray);
$array = json_decode('[{"id":261,"name":"Dobre","province_id":2},{"id":1128,"name":"Dobre","province_id":7},{"id":1992,"name":"Dobre Miasto","province_id":14}]');

for ($i=0; $i<=count($array); $i++) {
  echo $array [$i]->name;
}
var obj = JSON.parse('[{"id":261,"name":"Dobre","province_id":2},{"id":1128,"name":"Dobre","province_id":7},{"id":1992,"name":"Dobre Miasto","province_id":14}]'); 

alert(obj[0]['id']) // it will return 261
    foreach ($villages as $key => $value) {
      echo "<a href='".$value["id"]."'>".$value["name"]."</a><br>";
    }
public function query($name) {
    $villages = Village::select('id','name', 'province_id')->where('name', 'like', '%' . $name . '%')->get();
    $returnQuery = '';

    try {
        $villages = json_decode($villages, true);
        for ($i=0; $i < count($villages); $i++) {
          $returnQuery = '<a href="">' . $villages[$i]['name'] . '</a>';
        }
    } catch (Exception $e) {
        // Always good idea to catch json to prevent error if bad data is pass.
    }

    if ($returnQuery != '') {
      return $returnQuery;
    } else {
      return "Nic nie mam";
    }
}