₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,331,232 members, 8,449,291 topics. Date: Tuesday, 21 July 2026 at 02:42 PM

Toggle theme

Please I Need Help With This Array - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingPlease I Need Help With This Array (322 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 ArrayHow To Sort Array In Java With User Defined Method ?How To Allocate Space For Ragged Array Using Calloc() Or Malloc? HELP!234

Explore More Than FREE VERSION! Get The Premium Version!Who Can Build A Tracking Website? Let's TalkPlease Is It Possible To Download One Application Twice On One Device