title: How to produce valid json output which includes arrays of text
body: <p>I created a mySQL database with phpMyAdmin in my local server. In this database I stored the names and the favourite NBA teams of my friends (along with an id as a primary key for the database). However, some of my friends may have two favourite teams. I wrote and run the following php script to retrieve these data from the one table I have in the database (<em>I know that it is not the best way to have one table for many-to-many relationship</em>) and project them on my local web server (XAMPP):</p>
  
  <p>
  
  <pre><code>$dbServername = 'localhost';
  $dbUsername = 'root';
  $dbPassword = '';
  $dbName = 'Friends';
  
  $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
  
  header('Content-Type: application/json');
  
  $sql = 'SELECT * FROM friends;';
  $result = mysqli_query($conn, $sql);
  $resultCheck = mysqli_num_rows($result);
  
  arr = [];
  
  if ($resultCheck &gt; 0) {
      while ($row = mysqli_fetch_assoc($result)) {
          $arr[] = $row;
  
      }
  }
  
  echo json_encode($arr, JSON_PRETTY_PRINT);
  </code></pre>
  
  <p>However, in this way I take this output for one person: </p>
  
  <pre><code>   [{
          "id": "1",
          "name": "David Belton",
          "team": "Boston Celtics"
      }, {
          "id": "1",
          "name": "David Belton",
          "team": "Cleveland Cavaliers"
      }]
  </code></pre>
  
  <p>However, I would like to get this something like this as output for each specific person:</p>
  
  <pre><code>{
      "id": "1",
      "name": "David Belton",
      "team": ["Boston Celtics", "Cleveland Cavaliers"] 
  }
  </code></pre>
  
  <p>Is this possible in PHP?</p>
  
  <p>If so, is it recommended or should I do this with MySQL back at the database?</p>