₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,182 members, 8,510,568 topics. Date: Friday, 30 October 2026 at 03:49 PM

Toggle theme

Please I Need Help With This Array - Programming - Nairaland

Nairaland Forum › Science/Technology › Programming › Please I Need Help With This Array (332 Views)

1 Reply

Please I Need Help With This Array by cbrass(op): 12:52pm On Oct 04, 2021
Array
(
[A.txt] => 72
[B.txt] => 27
[C.txt] => 12
)
This is what I want to do:
1 sum all those values and give them to the first key which is [A.txt].
2. remove the [A.txt] then sum [b.txt] and [C.txt] then give the value to key [B.txt].
3 Remove [A.txt],[B.txt] then sum whatever is left and give it to [C.txt].

so that my result will look like this

Array
(
[A.txt] => 111,
[B.txt] => 39,
[C.txt] => 12
)
Language: PHP
Thanks a lot.
Re: Please I Need Help With This Array by efelico: 8:29pm On Oct 04, 2021
$temp_result=array();
$array=Array("[A.txt]" => 72,"[B.txt]" => 27,"[C.txt]" => 12);

$firstKey = array_key_first($array);
$sum=array_sum($array);
$temp_result=array_merge($temp_result,array($firstKey => $sum));


$output = array_slice($array, 1);
$secondKey = array_key_first($output);
$sum=array_sum($output);
$temp_result=array_merge($temp_result,array($secondKey => $sum));



$output = array_slice($array, 2);
$thirdKey = array_key_first($output);
$sum=array_sum($output);
$temp_result=array_merge($temp_result,array($thirdKey => $sum));


print_r($temp_result);
1 Reply

Write An Algorithm To Shuffle An Array • How To Sort Array In Java With User Defined Method ? • How To Allocate Space For Ragged Array Using Calloc() Or Malloc? HELP! • 2 • 3 • 4

Discover Your Best Accounting Software • Import Big Data Into Mysql Using Simple Trick • I Need A Professional To Write A Documentation