note 47537 added to ref.array

From: Date: Fri, 19 Nov 2004 14:44:39 +0000
Subject: note 47537 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-80744@lists.php.net to get a copy of this message
I was looking for a way to send product array data to a script via POST, the product information could vary slightly and have elements update or added depending on actions taken by the user. The data would then have to be repackaged as hidden fields (sessions were not an option) eg <FORM action="" method="post"> <INPUT name="product[0][product_id]" type="hidden" value="1"> <INPUT name="product[0][product_name]" type="hidden" value=".com"> <INPUT name="product[0][price]" type="hidden" value="14.99"> <INPUT name="product[0][duration]" type="hidden" value="5"> <INPUT name="product[1][product_id]" type="hidden" value="6"> <INPUT name="product[1][product_name]" type="hidden" value=".ie"> <INPUT name="product[1][price]" type="hidden" value="89.99"> <INPUT name="submitted" type="hidden" value="true"> <INPUT name="submit" type="submit"> </FORM> Below is a piece of code that I hope will save someone a bit of time. $arr_Products = $_REQUEST['product']; $int_OuterCntr = 0; foreach($arr_Products as $arr_ProductDetails) { foreach($arr_ProductDetails as $p) { $arr_Keys = array_keys($arr_ProductDetails, $p); // gets the keys to be used as the element name foreach($arr_Keys as $str_Keys) { echo'<INPUT name="product['.$int_OuterCntr.']['. $str_Keys.']" type="hidden" value="'.$p.'">'; } } $int_OuterCntr++; } ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47537 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47537&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47537&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47537&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47537&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47537&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47537&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47537&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80744) next »