Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,414 members, 7,822,926 topics. Date: Thursday, 09 May 2024 at 07:37 PM

Please I Need Help With This Array - Programming - Nairaland

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

Write An Algorithm To Shuffle An Array / How To Allocate Space For Ragged Array Using Calloc() Or Malloc? HELP! / Java Array Problem (2) (3) (4)

(1) (Reply)

Please I Need Help With This Array by cbrass(m): 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)

Ij.start.canon / Pls What Does It Entails in developing Crypto Exchange Platform / Deep Dive Into Variables Series(ii)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 6
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.