2011-12-29 160 views
-1

我找不到一种方法来读取URL中的变量或向量或文本形式的变量,以便它只返回正在重复的值 。在只有一个值的情况下,我需要显示它,或者在所有值都不同的情况下,我需要显示所有这些值。请帮帮我!这很紧急。 例如,如果我有1,2,3,1,4,我希望它显示1,并且如果我有1,2,3,4来显示它们全部。 需要帮助。算法php,计数和显示变量

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

请我需要帮助:(

回答

1

你可以用你的array_unique输入阵列上,看看有没有双打。如果array_unique后的数组是跟以前一样大,你应该打印所有值

据我所知,如果数组不包含所有唯一值,那么您希望打印多次出现的所有数据。如果您只想打印出现多次的值,可以首先检查array_count_values什么值发生多次并打印它们。

剩下的就是给你:)

+0

hoppa我有我的问题是错误的,我从URL意味着代替URL,对不起,你可以帮我一个码?请你帮我,这非常非常重要请 – UGD 2011-12-29 14:55:56

+0

好吧,至少你需要提供一个示例URL。 – hoppa 2011-12-29 15:04:19

+0

这是整个代码*我修改了它)...但没有工作... – UGD 2011-12-29 15:10:50

0

使用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);