开发者

How to process a large post array in PHP where item names are all different and not known in advance?

I have a PHP page that queries a DB to populate a form for the user to modify the data and submit.

The query returns a number of rows which contain 3 items:

  1. ImageID
  2. ImageName
  3. ImageDescription

The PHP page titles each box in the form with a generic name and appends the ImageID to it. Ie:

  • ImageID_03
  • ImageName_34
  • ImageDescription_22

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.

My backend PHP form processor that gets the data just sees it as one big array:

[imageid_2] =&开发者_C百科gt; 2
[imagename_2] => _MG_0214
[imageid_10] => 10
[imagename_10] => _MG_0419
[imageid_39] => 39
[imagename_39] => _MG_0420
[imageid_22] => 22
[imagename_22] => Curly Fern
[imagedescription_2] => Wibble
[imagedescription_10] => Wobble
[imagedescription_39] => Fred
[imagedescription_22] => Sally

I've tried to do an array walk on it to split it into 3 arrays which set places but am stuck:

// 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');

This fails with the error:

array_push(): First argument should be an array in...

Am I approaching this wrong?


Would it be possible to change the way the items are named on the form?

Current:

  • ImageID_03
  • ImageName_34
  • ImageDescription_22

Changed To:

  • ImageID[03]
  • ImageName[34]
  • ImageDescription[22]

This way it should come through the $_POST as three separate arrays meaning you can skip all that extra processing.


  1. I hate to edit many rows at once. It's usability fault
  2. If I go for it, I'd make it with such a form:

    <form method="POST">
    <input type="text" name="name[1]">
    <input type="text" name="desc[1]">
    <input type="text" name="name[2]">
    <input type="text" name="desc[2]">
    ...etc
    </form>

So, I'd have 2 arrays, $_POST['name'] and $_POST['desc'] where id used as a key

In your case I wouldn't use array_walk as it's just a syntax sugar, and make it with foreach to have full contorol over data processing.


I think you need

global $imageidarray;

to access a global array inside a function.


Something like this would work as well, assuming your three form fields are always submitted together:

$ids = preg_grep('/^imageid_(\d+)$/', array_keys($_POST)); // $ids now contains only the imageid_* keys

foreach($ids as $id) {
    $id = substr($ids, 8); // now contains the numeric id of the field
    echo $id, ": ", $_POST["imageid_$id"], "\n";
}

Not as efficient as using the array notation in the form names, but allows you to trivially extract just the ID numbers from one of the field names and then use it to access the other matching fields as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜