title: Save data passed to a PHP script via AJAX to a database
body: <p>I have an array:</p>
  
  <pre><code>var arr = [{"title": "lorem", "desc": "ipsum"}, ...];
  </code></pre>
  
  <p>that I try to save to a database via AJAX:</p>
  
  <pre><code>$.post("sample.php", {arr: arr}, function(data) { ... });
  </code></pre>
  
  <p>with the following code:</p>
  
  <pre><code>$data = $_POST['arr']
  $values = array();
  
  foreach($data as $value)
  {
      $values[] = '('. $value['title'] .', '. $value['desc'] .')';
  }
  
  $sql = "INSERT INTO locations (title, desc)
          VALUES " . implode( ',', $values );
  </code></pre>
  
  <p>It gives me the following error:</p>
  
  <pre><code>Invalid argument supplied for foreach()
  </code></pre>
  
  <p>I am not really a PHP dev, so I don't understant what goes wrong. Any ideas?</p>