title: Ajax return Json in Array, convert to string
body: <p>Hello I have code Ajax to return value from databse:</p>
  
  <pre><code>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 &amp;&amp; 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();
  }
  </code></pre>
  
  <p>And code php to connect with databse:</p>
  
  <pre><code>public function query($name) {
      $villages = Village::select('id','name', 'province_id')-&gt;where('name', 'like', '%' . $name . '%')-&gt;get();
  
      for ($i=0; $i&lt;=count($villages); $i++) {
        $returnQuery = $villages;
      }
  
      if ($returnQuery != '' || $returnQuery != null) {
        return '&lt;a href=""&gt;'.$returnQuery.'&lt;/a&gt;';
      } else {
        return "Nic nie mam";
      }
  }
  </code></pre>
  
  <p>This is code form to search value:</p>
  
  <pre><code>  {!! Form::text('string', NULL, ['class' =&gt; 'form-control', 'placeholder' =&gt; 'podaj frazę', 'onkeyup' =&gt; 'showResult(this.value)']) !!}
    &lt;div id="resultSearch"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Code php return me value in Json, look like this:</p>
  
  <blockquote>
    <p>[{"id":261,"name":"Dobre","province_id":2},{"id":1128,"name":"Dobre","province_id":7},{"id":1992,"name":"Dobre
    Miasto","province_id":14}]</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/zjjTG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zjjTG.png" alt="enter image description here"></a>
  I need return value in <code>a href</code>, and every record from the new line.
  Method <code>pluck()</code> and <code>toArray()</code> not work.</p>
  
  <p>Edit:
  I used <code>json_decode()</code>:</p>
  
  <pre><code>for ($i=0; $i&lt;=count($villages); $i++) {
    $returnQuery = $villages;
  }
  
  if (!empty($returnQuery)) {
    $someArray = json_decode($returnQuery, true);
    print_r($someArray);
    echo $someArray[0]["name"];
  }
  </code></pre>
  
  <p>But when I search name "Dobre" return only one record, but in databse I have 3 records with name "Dobre". Method <code>print_r()</code> return:</p>
  
  <blockquote>
    <p>Array ( [0] => Array ( [id] => 261 [name] => Dobre [province_id] => 2
    ) <a href="https://i.stack.imgur.com/zjjTG.png" rel="nofollow noreferrer">1</a> => Array ( [id] => 1128 [name] => Dobre [province_id] => 7 ) [2]
    => Array ( [id] => 1992 [name] => Dobre Miasto [province_id] => 14 ) )</p>
  </blockquote>
  
  <p>I don't know how I can set number of number of array in this line <code>[0]</code>:</p>
  
  <pre><code>echo $someArray[0]["name"];
  </code></pre>