Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

689
Views
¿Cómo contar los valores en una matriz y obtener el valor si es igual a un número PHP?

tengo una matriz:

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

Quiero contar cuántas veces un número es igual en la matriz y si el recuento de números iguales es 2, inserte el valor en otra matriz.

asi que:

en este caso, 1 es igual a 2 veces, por lo que quiero insertar 1 en una matriz.

¿Cómo logro esto?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Creo que puedes probar esto:

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

0

Espero que esto te ayude, aquí estamos usando la función array_count_values para contar los valores de una matriz.

Pruebe este fragmento de código aquí

 <?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 Report

0

¡Aquí tienes!

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

¡Pase su matriz de entrada y su marca de corte!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!