如何判断任何数组键的值是否是我要查找的值?
我有一个像这样的数组:
$cart = Array (
[0] => Array ( [TypeFlag] => S [qty] => 2 [denom] => 50 [totalPrice] => 100 )
[1] => Array ( [TypeFlag] => V [qty] => 1 [denom] => 25 [totalPrice] => 25 )
[2] => Array ( [TypeFlag] => C [qty] => 1 [denom] => 25 [totalPrice] => 25 )
)
除了循环遍历所有数组并一次检查一个数组之外,是否有任何方法可以确定其中任何一个的 TypeFlag 值是否为 S?
I have an array of arrays like this:
$cart = Array (
[0] => Array ( [TypeFlag] => S [qty] => 2 [denom] => 50 [totalPrice] => 100 )
[1] => Array ( [TypeFlag] => V [qty] => 1 [denom] => 25 [totalPrice] => 25 )
[2] => Array ( [TypeFlag] => C [qty] => 1 [denom] => 25 [totalPrice] => 25 )
)
Is there any way, short of looping through all of them and checking one at a time, to determine if the TypeFlag value for any of them is S?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
这将返回
TypeFlag
值为S
的子数组的键。但是,在找到与您的搜索模式匹配的第一个子数组后,这将停止。不确定您想要的输出是什么以及预期有多少结果。如果你能提供更多信息,我可以给你一个更准确的例子。Try this:
This would return the key of the sub-array that has a
TypeFlag
value ofS
. However, this would stop after it finds the first sub-array that matches your search pattern. Not sure what your desired output is tho and how many results are expected. If you can provide more info, I can give you a more accurate example.给定一个为数组中每个元素返回
TypeFlag
的函数:您可以将该函数应用于数组中的每个元素:
并查看
S
是否在该数组中:Given a function that returns the
TypeFlag
for each element of your array:You could apply that function to each element in the array:
and see if
S
is in that array: