Changing the array key names

I have a series of forms on my page which has content generated by my database. When I click on submit they look like this:

array
  'name2' => string 'PHP' (length=3)
  'publish2' => string 'YES' (length=3)
  'submit2' => string 'submit' (length=6)
  'hidden2' => string '2' (length=1)
  'hidden002' => string 'PHP' (length=3)

or like this:

array
  'name5' => string 'Web development' (length=15)
  'publish5' => string 'YES' (length=3)
  'submit5' => string 'submit' (length=6)
  'hidden5' => string '5' (length=1)
  'hidden005' => string 'Web development' (length=15)

What I want to be able to do is change the key names on submission:

array
  'first' => string 'Web development' (length=15)
  'second' => string 'YES' (length=3)
  'third' => string 'submit' (length=6)
  'fourth' => string '5' (length=1)
  'fifth' => string 'Web development' (length=15)

Looking elsewhere on the web there are guides about how to change the key names if they are already known [http://www.hotscripts.com/forums/php/37988-change-array-key.html] but here my forms are created dynamically so I won’t know the key names until they are submitted

Why are you not able to simply change the name attribute in each form element?


<input type="text" name="first">

or just make your dynamic entries arrays…

but here my forms are created dynamically so I won’t know the key names until they are submitted

Oh, I missed that part.

Well will you know for certain the order in which they are being submitted then?

My question would be why are you sending different names to the form? Name can be the same over and over again on the page – only ID needs to be unique and have the numbers attached. Even “generated by the database” that makes little if any sense… or are you doing multiple identical fields inside a single form? If so, you may want to consider using array based form names.

name=“element[1][name]”
name=“element[1][publish]”

Which would resolve to $_POST[‘element’][1][‘name’] for example.

This can no doubt be improved on, but if you do know the order in which your form elements are going to be returned:


// what is incoming - all you know is the order, not the key names
$_POST = array(
'name1' => 'Bob' ,
'name2' => 'The' ,
'name3' => 'Builder' ,
);

// the rewrite keys in the correct order
$newkeys = array('first', 'middle', 'last');

// put this function into a file and include it

function newKeys($existing, $newkeys){

// a really simple check that the arrays are the same size
  if(count($existing)  !== count($newkeys)) 
     return false; // or pipe out a useful message, or chuck exception

$data = array();  // set up a return array
$i = 0 ;
foreach( $existing as $k=>$v){
$data[$newkeys[$i]] = $v;  // build up the new array
$i++;
}
return $data; // return it
}


var_dump(newKeys($_POST, $newkeys) );

gives:

array
‘first’ => string ‘Bob’ (length=3)
‘middle’ => string ‘The’ (length=3)
‘last’ => string ‘Builder’ (length=7)

Although to be honest, you might as well have built your sql statement inside that loop as reassign the key names, but only you know the full extent of your challenge.

Lets take a step back.

I want to display multiple forms on the same page. There are user details which I want the admin to be able to edit. But rather than have a list of links which the admin has to click on and then goes to a separate page I want all the forms on one page.

Yes, it is possible to have the same form inputs on the same page but the problem arises when a form validation error is thrown and then the code for one input means the code for all inputs will be the same:

value="isset($_POST['nameField'])? print ($_POST['nameField']): null;"

Which is why I was giving the name attribute values unique numbers… hence the array question… taking a step back… is there a better way of organising my form code to allow multiple forms on the same page?

Set a value on your submit, then you know which submit was clicked… or put a unique value in a hidden form element on each form, letting you know which form was sent. That way they can all have the same name, but you can still tell which one was sent.

Having the same name values for different forms is fine for submitting them. The problem comes after errors are produced. Any altered text placed in the input values by the code above will change on every form unless the input values are different. Having a unique submit name value won’t alter that.

That doesn’t make any sense, if you know what ‘form’ they are from, you only use them to populate THAT form… because none of the other forms were submitted they shouldn’t even HAVE values.

thanks for that by the way it works well