CosmosRP Posted February 9, 2012 Share Posted February 9, 2012 Hey guys: If I some multi-dimensional arrays, like so: array { array { [0]=> "Path1" [1]=> "10" } } array { array { [0]=> "Path2" [1]=> "16" } array { [0]=> "Path3" [1]=> "110" } } How can I add all of the values of key 1? I know its a foreach loop, but I can't figure it out... would it be this? Quote Link to comment https://forums.phpfreaks.com/topic/256753-adding-array-values/ Share on other sites More sharing options...
Zane Posted February 9, 2012 Share Posted February 9, 2012 This is two multi-dimensional arrays.. but yeah, assuming you had one, you'd use a foreach $array = //multidimensional array $count = 0; foreach($array as $value) { if( is_array($value) { $count += $value[1]; } } Quote Link to comment https://forums.phpfreaks.com/topic/256753-adding-array-values/#findComment-1316206 Share on other sites More sharing options...
Maq Posted February 9, 2012 Share Posted February 9, 2012 You can also use array_walk or a recursive call. Quote Link to comment https://forums.phpfreaks.com/topic/256753-adding-array-values/#findComment-1316275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.