title: How to email Dynamic Array in PHP
body: <p>I've checked so many questions and forums but I couldn't figure out how to email an array.</p>
  <p>This will be an Order Form for products. Users can add new order rows by jquery.</p>
  <p>So here is my shortened example;</p>
  <p>form.php</p>
  <pre><code>&lt;input type=&quot;text&quot; name=&quot;array[][id]&quot; /&gt;
  &lt;input type=&quot;text&quot; name=&quot;array[][partno]&quot; /&gt;
  </code></pre>
  <p>on email.php;<br />
  I can see that, it Posts all the array by</p>
  <pre><code>&lt;?php var_dump($_POST['array']); ?&gt;
  </code></pre>
  <p>But i couldn't figure out how to email it.<br />
  This is what i tried in email.php which is wrong ;</p>
  <pre><code>$id = $_POST['array'][]['id'];
  $partno = $_POST['array'][]['partno'];
  </code></pre>
  <p>and</p>
  <pre><code>$email_message .= &quot;Id: &quot;.clean_string($id).&quot;\n&quot;;
  $email_message .= &quot;Part No: &quot;.clean_string($partno).&quot;\n&quot;;
  </code></pre>
  <p>What should I do to send the values in that array</p>
  <p>EDIT:<br />
  I tried but I couldn't figure out where to put @onetrickpony codes. Here is the whole code of email.php</p>
  <pre><code>&lt;?php 
  if(isset($_POST['email'])) {
   
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = &quot;a@a.com&quot;;
  $email_subject = &quot;order&quot;;
  
  function died($error) {
      // your error code can go here
      echo &quot;We are very sorry, but there were error(s) found with the form you submitted. &quot;;
      echo &quot;These errors appear below.&lt;br /&gt;&lt;br /&gt;&quot;;
      echo $error.&quot;&lt;br /&gt;&lt;br /&gt;&quot;;
      echo &quot;Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;&quot;;
      die();
  }
   
  // validation expected data exists
  if(!isset($_POST['first_name']) ||
      !isset($_POST['last_name']) ||
      !isset($_POST['email']) ||
      !isset($_POST['telephone']) ||
      !isset($_POST['array'])) {
          
          died('We are sorry, but there appears to be a problem with the form you submitted.');       
      }
   
  $first_name = $_POST['first_name']; // required
  $last_name = $_POST['last_name']; // required
  $email_from = $_POST['email']; // required
  $telephone = $_POST['telephone']; // not required
  
  $error_message = &quot;&quot;;
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
  $error_message .= 'The Email Address you entered does not appear to be valid.&lt;br /&gt;';
  }
  $string_exp = &quot;/^[A-Za-z .'-]+$/&quot;;
  if(!preg_match($string_exp,$first_name)) {
  $error_message .= 'The First Name you entered does not appear to be valid.&lt;br /&gt;';
  }
  if(!preg_match($string_exp,$last_name)) {
  $error_message .= 'The Last Name you entered does not appear to be valid.&lt;br /&gt;';
  }
  if(strlen($error_message) &gt; 0) {
  died($error_message);
  }
  $email_message = &quot;Form details below.\n\n&quot;;
   
  function clean_string($string) {
    $bad = array(&quot;content-type&quot;,&quot;bcc:&quot;,&quot;to:&quot;,&quot;cc:&quot;,&quot;href&quot;);
    return str_replace($bad,&quot;&quot;,$string);
  }
   
  $email_message .= &quot;First Name: &quot;.clean_string($first_name).&quot;\n&quot;;
  $email_message .= &quot;Last Name: &quot;.clean_string($last_name).&quot;\n&quot;;
  $email_message .= &quot;Email: &quot;.clean_string($email_from).&quot;\n&quot;;
  $email_message .= &quot;Telephone: &quot;.clean_string($telephone).&quot;\n&quot;;
  // create email headers
  $headers = 'From: '.$email_from.&quot;\r\n&quot;.
  'Reply-To: '.$email_from.&quot;\r\n&quot; .
  'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  ?&gt;
  &lt;?php var_dump($_POST['array']); ?&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>
  <hr />
  <h3>Solution</h3>
  <p>As I understand it was unnecessary to use <code>array[][input_name]</code> ..<br />
  I changed input names as;</p>
  <pre><code>&quot;partno[]&quot; ..etc
  </code></pre>
  <p>also added</p>
  <pre><code>!isset($_POST['partno']) || ..etc and,
  $partno = $_POST['partno']; .. etc
  </code></pre>
  <p>to necessary places,<br />
  and removed;</p>
  <pre><code>!isset($_POST['array'])
  </code></pre>
  <p>and for the Key part;</p>
  <pre><code>foreach( $partno as $key =&gt; $no ) {
  $email_message .= $no;
  $email_message .= $partnumber[$key];
  $email_message .= $quantity[$key];
  $email_message .= $partname[$key].&quot;\n&quot;;
  }
  </code></pre>
  <p>According to the answer of @Jeffrey Blake in the question topic;<br />
  <a href="https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array?rq=1">How to get form input array into PHP array</a></p>