当键未知时如何查找关联数组的第一个/第二个元素?

发布于 2024-09-29 12:22:27 字数 1554 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(3

你不是我要的菜∠ 2024-10-06 12:22:28

使用这些函数设置内部数组指针:

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

reset($groups);
echo current($groups); //the first one
end($groups);
echo current($groups); //the last one

如果你想要最后一个/第一个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

reset($groups);
echo current($groups); //the first one
end($groups);
echo current($groups); //the last one

If you wanna have the last/first key then just do something like $tmp = array_keys($groups); .

迷乱花海 2024-10-06 12:22:28
$array = array('Alpha' => 1.1,'Bravo' => 2.2,'Charlie' => 3.3,'Delta' => 4.4,'Echo' =>5.5, 'Golf' => 6.6);

$pos = 3;

function getAtPos($tmpArray,$pos) {
 return array_splice($tmpArray,$pos-1,1);
}

$return = getAtPos($array,$pos);

var_dump($return);

$array = array('Alpha' => 1.1,'Bravo' => 2.2,'Charlie' => 3.3,'Delta' => 4.4,'Echo' =>5.5, 'Golf' => 6.6);

$pos = 3;

function getAtPos($tmpArray,$pos) {
    $keys = array_keys($tmpArray);
    return array($keys[$pos-1] => $tmpArray[$keys[$pos-1]]);
}

$return = getAtPos($array,$pos);

var_dump($return);

编辑

假设第一个元素 $pos = 1,但通过将函数中的 $pos-1 引用更改为 $pos 可以轻松更改 $pos = 0

$array = array('Alpha' => 1.1,'Bravo' => 2.2,'Charlie' => 3.3,'Delta' => 4.4,'Echo' =>5.5, 'Golf' => 6.6);

$pos = 3;

function getAtPos($tmpArray,$pos) {
 return array_splice($tmpArray,$pos-1,1);
}

$return = getAtPos($array,$pos);

var_dump($return);

OR

$array = array('Alpha' => 1.1,'Bravo' => 2.2,'Charlie' => 3.3,'Delta' => 4.4,'Echo' =>5.5, 'Golf' => 6.6);

$pos = 3;

function getAtPos($tmpArray,$pos) {
    $keys = array_keys($tmpArray);
    return array($keys[$pos-1] => $tmpArray[$keys[$pos-1]]);
}

$return = getAtPos($array,$pos);

var_dump($return);

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

冷情 2024-10-06 12:22:28

您可以使用 array_keys,具体取决于您的数组有多大。

echo $groups[( array_keys( $groups )[1] )];

You could use array_keys, depending on how big your array is.

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