0
\$\begingroup\$

I have an HTML form being populated by a table of customer requests. The user reviews each request, sets an operation, and submits. The operation gets set into a post arg as follow

$postArray = array_keys($_POST);

[1]=>Keep [2]=>Reject [3]=>Reject [4]=>Ignore ["item id"]=>"operation"

This is how I am parsing my Post args... it seems awkward, the unused $idx. I am new to PHP, is there a smoother way to do this?

$postArray = array_keys($_POST); 
foreach($postArray as $idx => $itemId) { 
 $operation = $_POST[$itemId];
 echo "$itemId $operation </br>";
 ...perform operation...
}
asked Apr 16, 2012 at 22:53
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

It looks like you may want to unset the 'item id' entry in your post array before you do the processing:

unset($_POST['item id']);

Then you can use foreach to loop over the key (idx) and value (operation).

foreach ($_POST as $idx => $operation)
{
 echo $idx . ' ' . $operation . '</br>';
 // perform operation.
}

The array_keys function was unnecessary because foreach can be used with associative arrays handling both their keys and values.

answered Apr 17, 2012 at 2:55
\$\endgroup\$
1
\$\begingroup\$

You dont need to grab the the keys separately.

foreach($_POST as $key => $value) { 
 echo "$key $value </br>";
}
answered Apr 17, 2012 at 8:39
\$\endgroup\$
1
  • \$\begingroup\$ My answer already covered this. Adding another answer with the same information does not seem helpful. \$\endgroup\$ Commented Apr 17, 2012 at 11:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.