
"What is the best way to set up an array from a form so that I don't have to loop through every key-value on the PHP back end. (I would like to test if empty before using each key)"
"So taking a normal form query like this: cat1=1&cat2=2&cat3=3&type1=1&type2=2&type3=3&sub1=1&sub2=2&sub3=3"
"And processing it more like an object setup like this: cat [1,2,3] type [1,2,3] sub [1,2,3] So if category is empty (cat) but will still be a POST variable, I can search all categories because I know it is empty"
Use bracket notation for related inputs (cat[], type[], sub[]) so the backend receives $_POST['cat'], $_POST['type'], and $_POST['sub'] as arrays. Apply array_filter($_POST['cat']) to remove empty entries and use empty() or count() to detect no-selection cases. Use numeric indices (cat[0], cat[1]) or associative keys when positional mapping matters, and array_values() to reindex after filtering. Alternatively send a structured JSON payload from JavaScript and decode with json_decode on the server to preserve grouping. Iterate only over non-empty arrays and use prepared statements for inserting multiple rows.
Read at SitePoint Forums | Web Development & Design Community
Unable to calculate read time
Collection
[
|
...
]