【问题标题】:How to find first/second element of associative array when keys are unknown?当键未知时如何找到关联数组的第一个/第二个元素?
【发布时间】:2011-05-02 06:49:29
【问题描述】:

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

【问题讨论】:

    标签: php arrays associative-array


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      $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

      【讨论】:

        【解决方案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

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

        如果你想拥有最后一个/第一个 key,那么只需执行 $tmp = array_keys($groups); 之类的操作。

        【讨论】:

        • 当然,我可以只做$groupNames = array_keys($groups),然后我有$groupNames[0] 和$groupNames[1],谢谢。
        • @Edward 是的,我搜索的太远了。
        猜你喜欢
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多