htmlspecialchars()期望参数1为字符串

Case:

input_name = Abu 
input_address = Earth
hobbies[0] = read
hobbies[1] = sport
hobbies[2] = eat

Expected Result: Abu-Earth-read-sport-eat

but Result:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in /opt/lampp/htdocs/tester/listen.php on line 6

Abu-Earth-

Notice: Uninitialized string offset: 0 in /opt/lampp/htdocs/tester/listen.php on line 16

Notice: Uninitialized string offset: 1 in /opt/lampp/htdocs/tester/listen.php on line 17

Notice: Uninitialized string offset: 2 in /opt/lampp/htdocs/tester/listen.php on line 18

How to fix this? This my Code

index.php

<script src="https://code.jquery.com/jquery-3.1.1.min.js" charset="utf-8"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js" charset="utf-8"></script>

<form method="post" id="frm_test" name="frm_test">
  <input type="text" name="input_name" value="" required>
  <input type="text" name="input_address" value="" required>
  <input type="text" name="hobbies[0]" value="">
  <input type="text" name="hobbies[1]" value="">
  <input type="text" name="hobbies[2]" value="">
  <button type="submit">Submit Data</button>
</form>

<script type="text/javascript">
  $(function(){
    $('#frm_test').validate({
            submitHandler: function() {
        postData("frm_test","");
            }
        }
        );
  });

  function postData(frm, id){
    $.post("listen.php",$("#"+frm).serialize(),function(r){
      console.log(r);
    });
  }
</script>

listen.php

<?php
  function filterText($string)
  {
    $conn = mysqli_connect("localhost","root","","dbname");
    $filter = mysqli_real_escape_string($conn, stripslashes(strip_tags(htmlspecialchars($string,ENT_QUOTES))));
    return $filter;
  }

  foreach($_POST as $key=>$value){
    ${$key} = filterText($value);
  }

  echo $input_name."-";
  echo $input_address."-";
  echo $hobbies[0]."-";
  echo $hobbies[1]."-";
  echo $hobbies[2]."-";
 ?>

Your hobbies input field is an array. Modify your iteration for $_POST so that it handles arrays gracefully intelligently:

foreach($_POST as $key => $value){
    ${$key} = is_array($value) ? array_map("filterText", $value) : filterText($value);
}