title: PHP return array
body: <p>I have started to learn a mvc pattern and got simple example. But I can't manage to display data from mysql with return.</p>
  
  <p>Here in this code is 3 examples(right now trying to figure out how and why.)</p>
  
  <pre><code>    public function getPostsList()
  {
      // get data from database
  
      $stmt = $this-&gt;conn-&gt;prepare("SELECT * FROM visitors LIMIT 5");
      $stmt-&gt;execute();
      $result = $stmt-&gt;fetchAll();
  
      // Checking if my array works fine. And it works. (Check result 1.)
  
      $visitors = array();
      foreach($result as $user) {
       $visitors[] = $user;
      }
  
      print_r($visitors);
  
      // Trying to return my array (Don't work. See result 2.)
  
      return $visitors;
  
     // This is how data are shown in example (Line one ("Jungle Book" works. See result 3.)
  
      return array(
                   "Jungle Book" =&gt; new Book($visitors[3]['id'], $visitors[3]['ip'], $visitors[3]['date']),
                   "Moonwalker" =&gt; new Book("Moonwalker", "J. Walker", ""),
                   "PHP for Dummies" =&gt; new Book("PHP for Dummies", "Some Smart Guy", "")
           );
  
  
  }
  </code></pre>
  
  <p>Result 1:</p>
  
  <blockquote>
    <p>Array ( [0] => Array ( [id] => 1 [0] => 1 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 16:52:27 [2] => 2018-06-01 16:52:27 ) [1] => Array ( [id] => 2 [0] => 2 [ip] => 66.249.92.20 [1] => 66.249.92.20 [date] => 2018-06-01 16:52:52 [2] => 2018-06-01 16:52:52 ) [2] => Array ( [id] => 3 [0] => 3 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 17:00:14 [2] => 2018-06-01 17:00:14 ) [3] => Array ( [id] => 4 [0] => 4 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 17:01:46 [2] => 2018-06-01 17:01:46 ) [4] => Array ( [id] => 5 [0] => 5 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 18:31:47 [2] => 2018-06-01 18:31:47 ) ) </p>
  </blockquote>
  
  <p>Result 2:</p>
  
  <blockquote>
    <p>Notice: Trying to get property 'id' of non-object in 
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'id' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'ip' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'date' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'id' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'id' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'ip' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
    
    <p>Notice: Trying to get property 'date' of non-object in
    D:\xampp\htdocs\view\booklist.php on line 12</p>
  </blockquote>
  
  <p>Result 3:</p>
  
  <blockquote>
    <p>Title Author  Description </p>
    
    <p>4 217.166.253.194 2018-06-01 17:01:46</p>
    
    <p>Moonwalker    J. Walker    </p>
    
    <p>PHP for Dummies   Some Smart Guy</p>
  </blockquote>
  
  <p>Result 4 (var_dump)</p>
  
  <blockquote>
    <p>array(5) { [0]=> array(6) { ["id"]=> string(1) "1" [0]=> string(1) "1"
    ["ip"]=> string(15) "217.166.253.194" [1]=> string(15)
    "217.166.253.194" ["date"]=> string(19) "2018-06-01 16:52:27" [2]=>
    string(19) "2018-06-01 16:52:27" } [1]=> array(6) { ["id"]=> string(1)
    "2" [0]=> string(1) "2" ["ip"]=> string(12) "66.249.92.20" [1]=>
    string(12) "66.249.92.20" ["date"]=> string(19) "2018-06-01 16:52:52"
    [2]=> string(19) "2018-06-01 16:52:52" } [2]=> array(6) { ["id"]=>
    string(1) "3" [0]=> string(1) "3" ["ip"]=> string(15)
    "217.166.253.194" [1]=> string(15) "217.166.253.194" ["date"]=>
    string(19) "2018-06-01 17:00:14" [2]=> string(19) "2018-06-01
    17:00:14" } [3]=> array(6) { ["id"]=> string(1) "4" [0]=> string(1)
    "4" ["ip"]=> string(15) "217.166.253.194" [1]=> string(15)
    "217.166.253.194" ["date"]=> string(19) "2018-06-01 17:01:46" [2]=>
    string(19) "2018-06-01 17:01:46" } [4]=> array(6) { ["id"]=> string(1)
    "5" [0]=> string(1) "5" ["ip"]=> string(15) "217.166.253.194" [1]=>
    string(15) "217.166.253.194" ["date"]=> string(19) "2018-06-01
    18:31:47" [2]=> string(19) "2018-06-01 18:31:47" } }</p>
  </blockquote>
  
  <p>booklist.php</p>
  
  <pre><code>    &lt;?php
  
      foreach ($books as $title =&gt; $book)
      {
          echo '&lt;tr&gt;&lt;td&gt;&lt;a href="index.php?book='.$book-&gt;id.'"&gt;'.$book-&gt;id.'&lt;/a&gt;&lt;/td&gt;&lt;td&gt;'.$book-&gt;ip.'&lt;/td&gt;&lt;td&gt;'.$book-&gt;date.'&lt;/td&gt;&lt;/tr&gt;';
      }
  
  ?&gt;
  </code></pre>
  
  <p>Book Class</p>
  
  <pre><code>    &lt;?php
  
  require_once('controller/Config.php');
  
  class Book {
      public $id;
      public $ip;
      public $date;
  
      public function __construct($id, $ip, $date)
      {
        $this-&gt;id = $id;
          $this-&gt;ip = $ip;
          $this-&gt;date = $date;
      }
  }
  
  ?&gt;
  </code></pre>