解析帖子的最后一个数字
好吧,我有一篇文章看起来有点像这样,
[optional_premium_1] => Array
(
[0] => 61
)
[optional_premium_2] => Array
(
[0] => 55
)
[optional_premium_3] => Array
(
[0] => 55
)
[premium_1] => Array
(
[0] => 33
)
[premium_2] => Array
(
[0] => 36 )
[premium_3] => Array
(
[0] => 88 )
[premium_4] => Array
(
[0] => 51
)
我如何从中获得最高的数字。例如,可选的“可选_premium_”最高为 3,而“premium_”可选最高为 4。我如何找到此 $_POST 中的最高值
Ok so i have a post that looks kind of this
[optional_premium_1] => Array
(
[0] => 61
)
[optional_premium_2] => Array
(
[0] => 55
)
[optional_premium_3] => Array
(
[0] => 55
)
[premium_1] => Array
(
[0] => 33
)
[premium_2] => Array
(
[0] => 36 )
[premium_3] => Array
(
[0] => 88 )
[premium_4] => Array
(
[0] => 51
)
how do i get the highest number out of the that. So for example, the optional "optional_premium_" highest is 3 and the "premium_" optional the highest is 4. How do i find the highest in this $_POST
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 array_key_exists(),也许是这样的:
You could use array_key_exists(), perhaps something like this:
在回答之前,我有 1 个问题,分为 2 个部分,这就是为什么你要使用嵌入式数组?如果您使用以下标准符号,您的文章会简单得多:
除非您出于某种原因专门使用数组构建这篇文章。这样你就可以正常使用数组键作为变量名和数组值。
因此:
您可以使用
//then 再次循环来比较每个
应该为您提供最高值的值,但随后您必须返回并比较它们以执行您的最终计划。
您还可以将它们分成 2 个单独的数组,并假设它们是按顺序添加的,只需使用 end() http://php.net/manual/en/function.end.php
I have 1 question with 2 parts before I answer, and that is why are you using embedded arrays? Your post would be much simpler if you used a standard notation like:
unless you are specifically building this post with arrays for some reason. That way you could use the array key as the variable name and the array value normally.
So given:
you could use
//then loop through again to compare each one
that should get you the highest one, but then you have to go back and compare them to do whatever your end plan for it was.
You could also break those into 2 separate arrays and assuming they are added in order just use end() http://php.net/manual/en/function.end.php
循环遍历所有 POST 数组元素,挑选出键名称与“name_number”模式匹配的元素,并保存键名称中数字部分最大的元素。这是执行此操作的 PHP 脚本:
这是脚本的输出:
Loop through all POST array elements, pick out elements having key names matching "name_number" pattern and save the ones having the largest number portion of the key names. Here is a PHP script which does it:
Here is the output from the script:
虽然效果不佳,但您可以尝试类似的方法。
问题是这仅适用于一组类别。它很可能会在整个脚本中给出错误。
理想情况下,您需要重新组织您的帖子,使每个数组类似于
然后只需
foreach
并使用$array["key"]
进行搜索。Though ineffective, you could try something like
The problem being that this will only work for one set of categories. It will most likely give errors throughout the script.
Ideally, you would want to reorganize your post, make each array be something like
Then just
foreach
and use$array["key"]
to search.