Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
Find first number of each element in array and add them up without using strings
   public function pom() {
        $array = [25, 12, 5, 82, 15];
        $result = 0;
        for($i = 0; $i < count($array); $i++) {
            $first = str_split($array[$i]);
            if(is_numeric($first[0])) 
            $result += $first[0];
        }
        return $result;
    }

it works like this but question says i cannot use strings . Does it mean i cannot use str_split ?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can do that with substr array_map() and array_sum(),

<?php
$array = [25, 12, 5, 82, 15];
function sum_first_digit($array) {
   $sum = array_sum(array_map(function ($a){return 
substr($a,0,1);}, $array));
   return $sum;
 }
 echo sum_first_digit($array);
?>

Output: 17

over 4 years ago · Santiago Trujillo Denunciar

0

Extracting the first digit without the use of a string function means in my opinion only using calculations. Example:

$n = 781234;
$result = intdiv($n,pow(10,floor(log10($n)))); // 7
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda