Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

698
Visualizações
How to count the values in an array and get the value if its equal to a number PHP?

I have an array:

array:3 [▼
  0 => 1
  1 => 1
  2 => 2
]

I want to count how many times a number is equal in the array and if the count of equal numbers is 2 push the value into an other array.

so:

in this case 1 is equal 2 times, so i want to push 1 into an array.

How do i achieve this?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I think you can try this:

$tempArr = $secondArr = array();
foreach($firstArr as $value) {
   if(in_array($value,$tempArr)) {
       $secondArr[] = $value;
   }
   $tempArr[] = $value;
}
over 4 years ago · Santiago Trujillo Relatório

0

Hope this will help you out, here we are using array_count_values function for counting the values of an array.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$array=array(
    1,
    1,
    2
);
$result=array();
foreach(array_count_values($array) as $key => $value)
{
    if($value==2)//checking whether the count of number is equal to 2
    {
        $result[]=$key;//pushing value in array
    }
}
print_r($result);
over 4 years ago · Santiago Trujillo Relatório

0

Here you go!

function getMyArray($input_arr, $threshold){
    $final_arr = [];
    $arr_counts = array_count_values($input_arr);
    foreach( $arr_counts as $key => $value ){
        if ( $value == $threshold ){
            $final_arr[] = $key;
        }
    }
    return $final_arr;
}

Pass your input array and your cutoff mark!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda