如何在 PHP 中循环访问一系列具有特定前缀的响应变量?

发布于 2024-09-29 00:15:58 字数 207 浏览 1 评论 0原文

我正在将表单发布到 php 脚本。该表单包含动态数量的名为cardObjectX 的字段,其中X 是一个计数器。示例:cardObject1、cardObject2 等。我需要循环遍历我的 php 脚本中的所有 cardObject 字段,但因为我们不知道任何给定的帖子会有多少个,所以我们无法对字段名称进行硬编码。

有没有办法可以获取以 cardObject 开头的所有字段的数组?

I'm posting a form to a php script. The form contains a dynamic number of fields named cardObjectX, where X is a counter. Example: cardObject1, cardObject2, and so on. I need to loop through all the cardObject fields in my php script, but because we don't know how many there will be for any given post, we can't hard-code the field names.

Is there a way I can grab an array of all the fields that start with cardObject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

地狱即天堂 2024-10-06 00:15:58

这应该可以帮助您开始:

foreach($_POST as $key=>$value) {
   if(strpos($key,"cardObject")!==FALSE) {
        //do something with this cardObject...
   }
}

this should help you get started:

foreach($_POST as $key=>$value) {
   if(strpos($key,"cardObject")!==FALSE) {
        //do something with this cardObject...
   }
}
碍人泪离人颜 2024-10-06 00:15:58
<input name="cardObject[1]" value="">

在输入中使用这种命名风格可以像这样在 php 中以数组形式访问这些输入:

$_POST['cardObject'][1]

或者像这样循环访问每个 cardObject:

foreach($_POST['cardObject'] as $cardObject){

}
<input name="cardObject[1]" value="">

using this naming style in your inputs makes it possible to access these inputs as an array in php like this:

$_POST['cardObject'][1]

or loop throug every cardObject like this:

foreach($_POST['cardObject'] as $cardObject){

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文