如何从 $_POST 数组返回特定键?
我想检查 $_POST 中是否有包含字符串的键。该字符串不会是完整的密钥,而只是密钥的一部分。 (即搜索字符串 =“delRowID”,$_POST 键 =“delRowID_16”)。我尝试使用 array_keys($_POST,"delRowID"),但它从未返回任何内容。
代码
print_r($_POST);
print_r(array_keys($_POST,"delRowID"));
返回
Array ( [delRowID] => 29 [qAction] => [elmUpdateId] => [elmTtl] => [elmDesc] => [elmStr] => ) Array ( )
I would like to check to see if there are any keys in $_POST that contain a string. The string will not be the full key, only part of the key. (ie. search string = "delRowID", $_POST key = "delRowID_16"). I have tried to use array_keys($_POST,"delRowID"), but it has never returned anything.
CODE
print_r($_POST);
print_r(array_keys($_POST,"delRowID"));
RETURNS
Array ( [delRowID] => 29 [qAction] => [elmUpdateId] => [elmTtl] => [elmDesc] => [elmStr] => ) Array ( )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果这是通过表单发送的,请考虑将元素命名为数组元素。例如,
将以名为
$_POST['delRowID']
的数组形式出现,其中包含每个有效输入的元素。然而,这是一个人为的示例,它更适合其他输入类型。
对于复选框,最好这样做,它为每个成功的复选框创建一个包含值的数组,您可以轻松地循环遍历:
另请参阅:如何在 HTML
If this is being sent by a form, considering naming the elements as array elements. For example,
would come in as an array named
$_POST['delRowID']
with elements for each valid input.However, this is a contrived example which works better with other input types.
For checkboxes, it would be better done like this, which creates an array with a value for each successful checkbox that you can easily loop over:
See Also: How do I create arrays in a HTML <form>?
使用
array_keys()
执行循环并使用strpos()
检查密钥Do a loop using
array_keys()
and check the key withstrpos()
循环遍历
array_keys($_POST)
提供给您的密钥。对每个进行字符串匹配。另请注意,
array_keys($_POST,"delRowID")
搜索与“delRowID”值关联的键。Loop through the keys provided to you by
array_keys($_POST)
. Do a string match on each.Also, note that
array_keys($_POST,"delRowID")
searches for keys that are associated with a value of "delRowID".因为您正在搜索部分文本,所以您可以循环遍历它:
Because you are searching for partial text, you can loop through it:
另一种方式(扩展疯狂的答案):
Just another way (extending mads answer):