当键未知时如何查找关联数组的第一个/第二个元素?
在 PHP 中,当你有一个关联数组时,例如:
$groups['paragraph'] = 3
$groups['line'] = 3
当你不知道键的值时访问数组的第一个或第二个元素的语法是什么?
是否有类似 C# LINQ 语句中的内容,您可以这样说:
$mostFrequentGroup = $groups->first()?
或者
$mostFrequentGroup = $groups->getElementWithIndex(0)?
或者 我是否必须使用 foreach 语句并像我在此代码示例底部所做的那样将它们挑选出来:
//should return "paragraph"
echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph'));
//should return "line"
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'date', 'date', 'line', 'line', 'line'));
//should return null
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'wholeNumber', 'paragraph', 'paragraph'));
//should return "wholeNumber"
echo getMostFrequentlyOccurringItem(array('wholeNumber', '', '', ''));
function getMostFrequentlyOccurringItem($items) {
//catch invalid entry
if($items == null) {
return null;
}
if(count($items) == 0) {
return null;
}
//sort
$groups = array_count_values($items);
arsort($groups);
//if there was a tie, then return null
if($groups[0] == $groups[1]) { //******** HOW TO DO THIS? ***********
return null;
}
//get most frequent
$mostFrequentGroup = '';
foreach($groups as $group => $numberOfTimesOccurrred) {
if(trim($group) != '') {
$mostFrequentGroup = $group;
break;
}
}
return $mostFrequentGroup;
}
In PHP when you have an associative array, e.g.:
$groups['paragraph'] = 3
$groups['line'] = 3
what is the syntax to access the first or second element of the array when you don't know the value of the keys?
Is there something like in a C# LINQ statement where you can say:
$mostFrequentGroup = $groups->first()?
or
$mostFrequentGroup = $groups->getElementWithIndex(0)?
Or do I have to use a foreach statement and pick them out as I do at the bottom of this code example:
//should return "paragraph"
echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph'));
//should return "line"
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'date', 'date', 'line', 'line', 'line'));
//should return null
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'wholeNumber', 'paragraph', 'paragraph'));
//should return "wholeNumber"
echo getMostFrequentlyOccurringItem(array('wholeNumber', '', '', ''));
function getMostFrequentlyOccurringItem($items) {
//catch invalid entry
if($items == null) {
return null;
}
if(count($items) == 0) {
return null;
}
//sort
$groups = array_count_values($items);
arsort($groups);
//if there was a tie, then return null
if($groups[0] == $groups[1]) { //******** HOW TO DO THIS? ***********
return null;
}
//get most frequent
$mostFrequentGroup = '';
foreach($groups as $group => $numberOfTimesOccurrred) {
if(trim($group) != '') {
$mostFrequentGroup = $group;
break;
}
}
return $mostFrequentGroup;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用这些函数设置内部数组指针:
http://ch.php.net/manual/en/ function.reset.php
http://ch.php.net/manual/en/function .end.php
这个获取实际元素:
http://ch.php.net/manual/en/function.current.php
如果你想要最后一个/第一个key,然后只需执行类似
$tmp = array_keys($groups);
的操作。use these functions to set the internal array pointer:
http://ch.php.net/manual/en/function.reset.php
http://ch.php.net/manual/en/function.end.php
And this one to get the actual element:
http://ch.php.net/manual/en/function.current.php
If you wanna have the last/first key then just do something like
$tmp = array_keys($groups);
.或
编辑
假设第一个元素 $pos = 1,但通过将函数中的 $pos-1 引用更改为 $pos 可以轻松更改 $pos = 0
OR
EDIT
Assumes $pos = 1 for the first element, but easy to change for $pos = 0 by changing the $pos-1 references in the functions to $pos
您可以使用 array_keys,具体取决于您的数组有多大。
You could use array_keys, depending on how big your array is.