php while 循环遍历数组
我试图循环遍历子数组(它是多维数组的一部分)并检查是否有一对键/值。如果找到该对,我想返回找到它的子数组的键。
不幸的是,key() 函数似乎无法与 foreach 一起使用。
我如何更改此代码以使用 while 循环?
如果您有更好的建议请告诉我。
foreach ($subarray as $subkey => $subvalue) {
if ($subkey == 'key_value' AND $subvalue = 'value') {
return key($subarray);
}
}
数组键不是数字。这是一个示例:
$array['books'] = array('quantity' => 10, 'title' => 'Something')
$array['dvds'] = array('quantity' => 30, 'title' => 'Something else')
搜索名为“something”的“title”,该函数应返回“books”,因为这是找到子键/值对的键。
感谢您的帮助。
I'm trying to loop through a sub array (which is part of a multidimensional array) and check if there's a pair of key/value. If the pair is found, I want to return the key of the sub array in which it was found.
Unfortunately it seems the key() function doesn't work with foreach.
How would I change this code to use a while loop?
If you have a better suggestion let me know.
foreach ($subarray as $subkey => $subvalue) {
if ($subkey == 'key_value' AND $subvalue = 'value') {
return key($subarray);
}
}
The array keys are not numeric. Here's a example :
$array['books'] = array('quantity' => 10, 'title' => 'Something')
$array['dvds'] = array('quantity' => 30, 'title' => 'Something else')
Searching for a "title" called "something", the function should return "books" because that's the key where the pair of sub key/value is found.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)