-1

URL、ベクター、またはテキスト形式で変数を読み取って、繰り返される値だけを返す方法が見つかりません。値が 1 つだけの場合は表示する必要があり、値がすべて異なる場合はすべて表示する必要があります。

たとえば、1,2,3,1,4 がある場合は 1 を表示し、1,2,3,4 がある場合はそれらすべてを表示します。

$values = $_GET['intrebare'];
$count = count($values);

foreach($values as $val =>$array) 
{
    //echo $val . '<br/>';
    //var_dump($array);

    if(is_array($array))
    {
        var_dump($array);
    }
    else
    {
        echo $val;
    }
}
4

2 に答える 2

1

入力配列でarray_uniqueを使用して、doubleがないかどうかを確認できます。array_uniqueの後の配列が以前と同じ大きさである場合は、すべての値を出力する必要があります。

私の理解では、配列にすべての一意の値が含まれていない場合は、複数回発生するすべての値を出力する必要があります。複数回発生する値のみを出力する場合は、最初にarray_count_valuesを使用して、複数回発生する値を確認して印刷できます。

後は君しだい :)

于 2011-12-29T14:49:45.387 に答える
1

を使用array_count_valuesするのが最も簡単な方法ですが、探しているものを達成する方法を把握する必要がある場合は、冗長バージョンを使用してください。

$input = array(1, 2, 3, 4, 1);
$unique = array_unique($input);

// If $input and $unique are different in length, 
// there is one or more repeating values
if (count($input) !== count($unique)) {
    $repeat = array();

    // Sort values in order to have equal values next to each other
    sort($input);

    for ($i = 1; $i < count($input) - 1; $i++) {
        // If two adjacent numbers are equal, that's a repeating number.
        // Add that to the pile of repeated input, disregarding (at this stage)
        // whether it is there already for simplicity.
        if ($input[$i] === $input[$i - 1]) {
            $repeat[] = $input[$i];
        }
    }

    // Finally filter out any duplicates from the repeated values
    $repeat = array_unique($repeat);

    echo implode(', ', $repeat);
} else {
    // All unique, display all
    echo implode(', ', $input);
}

簡潔なワンライナー風のバージョンは次のようになります。

$input = array(1, 2, 3, 4, 1);
$repeat = array_keys(
    array_filter(
        array_count_values($input), 
        function ($freq) { return $freq > 1; }
    )
);

echo count($repeat) > 0 
        ? implode(', ', $repeat)
        : implode(', ', $input);
于 2012-01-04T13:37:14.877 に答える