如何找到关联数组的第一/第二元素时关键是未知? [英] How to find first/second element of associative array when keys are unknown?

查看:75
本文介绍了如何找到关联数组的第一/第二元素时关键是未知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,当你有一个关联数组,例如:

  $组['段落'] = 3
$组['行'] = 3

什么是访问数组的第一个或第二个元素,当你不知道该键的值的语法

有没有在C#LINQ声明类似,你可以说:

  $ mostFrequentGroup = $组 - >首先()?

  $ mostFrequentGroup = $组 - > getElementWithIndex(0)?

或者我必须使用foreach语句,并挑选出来,因为我在这个code例如底部做的:

  //应该返回段落
回声getMostFrequentlyOccurringItem(阵列('行','段落','段落'));//应该返回线
回声getMostFrequentlyOccurringItem(阵列('wholeNumber,日期,日期,行,行,行));//应该返回null
回声getMostFrequentlyOccurringItem(阵列('wholeNumber','wholeNumber','段','段'));//应该返回wholeNumber
回声getMostFrequentlyOccurringItem(阵列('wholeNumber','','',''));功能getMostFrequentlyOccurringItem($项目){    //抓无效项
    如果($项目== NULL){
        返回null;
    }
    如果(计数($项目)== 0){
        返回null;
    }    //分类
    $组= array_count_values​​($项目);
    arsort($组);    //如果有一条领带,然后返回null
    如果($组[0] == $组[1]){// ********如何做到这一点? ***********
        返回null;
    }    //获取最常见
    $ mostFrequentGroup ='';
    的foreach($群体为$组=> $ numberOfTimesOccurrred){
        如果(修剪($组)!=''){
            $ mostFrequentGroup = $组;
            打破;
        }
    }
    返回$ mostFrequentGroup;
}


解决方案

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

<一个href=\"http://ch.php.net/manual/en/function.reset.php\">http://ch.php.net/manual/en/function.reset.php

<一个href=\"http://ch.php.net/manual/en/function.end.php\">http://ch.php.net/manual/en/function.end.php

而这其中,以获得实际的元素:
<一href=\"http://ch.php.net/manual/en/function.current.php\">http://ch.php.net/manual/en/function.current.php

 复位($组);
回声电流($组); //第一个
端($组);
回声电流($组); //最后一个

如果你想有最后/第一的的然后就做类似 $ TMP = array_keys($组);

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;
}

解决方案

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); .

这篇关于如何找到关联数组的第一/第二元素时关键是未知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆