title: PDO - Insert large array to MySQL database
body: <p>I need some help to improve my current code. I have a huge array (about 20,000 objects inside it). The array looks like this:</p>
  
  <pre><code>  Array
  (
      [0] =&gt; Player Object
          (
              [name] =&gt; Aaron Flash
              [level] =&gt; 16
              [vocation] =&gt; Knight
              [world] =&gt; Amera
              [time] =&gt; 900000
              [online] =&gt; 1
          )
  
      [1] =&gt; Player Object
          (
              [name] =&gt; Abdala da Celulose
              [level] =&gt; 135
              [vocation] =&gt; Master Sorcerer
              [world] =&gt; Amera
              [time] =&gt; 900000
              [online] =&gt; 1
          )
  
      [2] =&gt; Player Object
          (
              [name] =&gt; Ahmudi Segarant
              [level] =&gt; 87
              [vocation] =&gt; Elite Knight
              [world] =&gt; Amera
              [time] =&gt; 900000
              [online] =&gt; 1
          )
  
      [3] =&gt; Player Object
          (
              [name] =&gt; Alaskyano
              [level] =&gt; 200
              [vocation] =&gt; Royal Paladin
              [world] =&gt; Amera
              [time] =&gt; 900000
              [online] =&gt; 1
          )
  
      [4] =&gt; Player Object
          (
              [name] =&gt; Aleechoito
              [level] =&gt; 22
              [vocation] =&gt; Knight
              [world] =&gt; Amera
              [time] =&gt; 900000
              [online] =&gt; 1
          )
  </code></pre>
  
  <p>And so on... with about 20,000 Player Object in total.</p>
  
  <p>Now I want to insert them all in to my database. I'd like to find a way to not loop through all players. It is causing a lot of performance issues and it's almost killing my computer. I'd like to make it in a single query, all at once.</p>
  
  <p>But how can I get the Player Object attributes, like the "name", "level" and "vocation" of each individual object without looping them through?</p>
  
  <p>This is what my code looks like:</p>
  
  <pre><code>// Insert player list to database
  $sql = $db-&gt;prepare("INSERT INTO players (name, level, vocation, world, month, today, online) VALUES (:name, :level, :vocation, :world, :time, :time, :online) ON DUPLICATE KEY UPDATE level = :level, vocation = :vocation, world = :world, month = month + :time, today = today + :time, online = :online");
  
  foreach ($players as $player) {
    $query = $sql-&gt;execute([
      ":name" =&gt; $player-&gt;name,
      ":level" =&gt; $player-&gt;level,
      ":vocation" =&gt; $player-&gt;vocation,
      ":world" =&gt; $player-&gt;world,
      ":time" =&gt; $player-&gt;time,
      ":online" =&gt; $player-&gt;online
    ]);
  }
  </code></pre>
  
  <p>Because right now on that foreach at the bottom, it is looping through 20,000 player objects in my array, and getting their names/level/vocation/world and so on.</p>
  
  <p>Is there a better way to do this?
  My way of doing it can't be the best solution. I can hear my PC is working overload and it feels as if it's about to crash.</p>