Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to process a large post array in PHP where item names are all different and not known in advance?
    text
    copied!<p>I have a PHP page that queries a DB to populate a form for the user to modify the data and submit.<br> The query returns a number of rows which contain 3 items:</p> <ol> <li>ImageID</li> <li>ImageName</li> <li>ImageDescription</li> </ol> <p>The PHP page titles each box in the form with a generic name and appends the ImageID to it. Ie:</p> <ul> <li>ImageID_03</li> <li>ImageName_34</li> <li>ImageDescription_22</li> </ul> <p>As it's unknown which images are going to have been retrieved from the DB then I can't know in advance what the name of the form entries will be. The form deals with a large number of entries at the same time.</p> <p>My backend PHP form processor that gets the data just sees it as one big array:</p> <pre><code>[imageid_2] =&gt; 2 [imagename_2] =&gt; _MG_0214 [imageid_10] =&gt; 10 [imagename_10] =&gt; _MG_0419 [imageid_39] =&gt; 39 [imagename_39] =&gt; _MG_0420 [imageid_22] =&gt; 22 [imagename_22] =&gt; Curly Fern [imagedescription_2] =&gt; Wibble [imagedescription_10] =&gt; Wobble [imagedescription_39] =&gt; Fred [imagedescription_22] =&gt; Sally </code></pre> <p>I've tried to do an array walk on it to split it into 3 arrays which set places but am stuck:</p> <pre><code>// define empty arrays $imageidarray = array(); $imagenamearray = array(); $imagedescriptionarray = array(); // our function to call when we walk through the posted items array function assignvars($entry, $key) { if (preg_match("/imageid/i", $key)) { array_push($imageidarray, $entry); } elseif (preg_match("/imagename/i", $key)) { // echo " ImageName: $entry"; } elseif (preg_match("/imagedescription/i", $key)) { // echo " ImageDescription: $entry"; } } array_walk($_POST, 'assignvars'); </code></pre> <p>This fails with the error:<br> array_push(): First argument should be an array in...</p> <p>Am I approaching this wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload