Anahtarlar bilinmediği zaman birleşmeli dizinin birinci / ikinci öğe bulmak nasıl?

0 Cevap php

PHP, bir ilişkisel dizi var, mesela:

$groups['paragraph'] = 3
$groups['line'] = 3

what is the syntax to access the first veya second element of the array when you don't know the value of the keys?

Eğer söyleyebiliriz C # LINQ deyiminde olduğu gibi bir şey var mı:

$mostFrequentGroup = $groups->first()?

veya

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

Or do I have to use a fveyaeach 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;
    }

    //sveyat
    $groups = array_count_values($items);
    arsveyat($groups);

    //if there was a tie, then return null
    if($groups[0] == $groups[1]) { //******** HOW TO DO THIS? ***********
        return null;
    }

    //get most frequent
    $mostFrequentGroup = '';
    fveyaeach($groups as $group => $numberOfTimesOccurrred) {
        if(trim($group) != '') {
            $mostFrequentGroup = $group;
            break;
        }
    }
    return $mostFrequentGroup;
}

0 Cevap