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

199
Views
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 answers
Answer question

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 Report

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 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!