如何从 $_POST 数组返回特定​​键?

发布于 2024-09-04 05:31:45 字数 400 浏览 4 评论 0原文

我想检查 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花落人断肠 2024-09-11 05:31:45

如果这是通过表单发送的,请考虑将元素命名为数组元素。例如,

<input type="checkbox" name="delRowID[16]" />
<input type="checkbox" name="delRowID[17]" />

将以名为 $_POST['delRowID'] 的数组形式出现,其中包含每个有效输入的元素。

然而,这是一个人为的示例,它更适合其他输入类型。

对于复选框,最好这样做,它为每个成功的复选框创建一个包含值的数组,您可以轻松地循环遍历:

<input type="checkbox" name="delRowID[]" value="16" />
<input type="checkbox" name="delRowID[]" value="17" />

另请参阅:如何在 HTML

中创建数组?

If this is being sent by a form, considering naming the elements as array elements. For example,

<input type="checkbox" name="delRowID[16]" />
<input type="checkbox" name="delRowID[17]" />

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:

<input type="checkbox" name="delRowID[]" value="16" />
<input type="checkbox" name="delRowID[]" value="17" />

See Also: How do I create arrays in a HTML <form>?

冰魂雪魄 2024-09-11 05:31:45

使用 array_keys() 执行循环并使用 strpos() 检查密钥

foreach (array_keys($_POST) as $key) {
  if (strpos($key, 'delRowId') === 0) {
    echo $key." found!";
  }
}

Do a loop using array_keys() and check the key with strpos()

foreach (array_keys($_POST) as $key) {
  if (strpos($key, 'delRowId') === 0) {
    echo $key." found!";
  }
}
野鹿林 2024-09-11 05:31:45

循环遍历 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".

败给现实 2024-09-11 05:31:45

因为您正在搜索部分文本,所以您可以循环遍历它:

foreach($_POST as $key => $value)
{
  if (strpos($key, 'delRowID') !== false)
  {
    echo $key;
    break;
  }
}

Because you are searching for partial text, you can loop through it:

foreach($_POST as $key => $value)
{
  if (strpos($key, 'delRowID') !== false)
  {
    echo $key;
    break;
  }
}
垂暮老矣 2024-09-11 05:31:45

另一种方式(扩展疯狂的答案):

if( getKey( 'delRowId', $_POST ) ){
    // delRow?
}

function getKey($stringToFind, $array) {
  foreach ($_POST as $key => $val) {
    if (strpos($stringToFind, $key) !== false) {
     return $val;
    } 
  }
  return false;
}

Just another way (extending mads answer):

if( getKey( 'delRowId', $_POST ) ){
    // delRow?
}

function getKey($stringToFind, $array) {
  foreach ($_POST as $key => $val) {
    if (strpos($stringToFind, $key) !== false) {
     return $val;
    } 
  }
  return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文