SeanHarding Posted November 21, 2011 Share Posted November 21, 2011 Hi have an array() in which I would like to manipulate certain elements: Array ( [TESCO] => Array ( [Veg] => Array ( [Potatoe] => 12 [Cabbage] => 24 [Cucumber] => 56 ) [Meat] => Array ( [Chicken] => 54 [beef] => 89 ) ) [ASDA] => Array ( [Veg] => Array ( [Potatoe] => 12 [Cabbage] => 24 [Cucumber] => 56 ) [Meat] => Array ( [Chicken] => 23 [beef] => 90 ) ) ) How would I go about adding the price of my meat section together e.g. Chicken + Beef from ASDA = 113 Also how to add/remove elements from Meat/Veg? Like adding Pork Cheers Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/ Share on other sites More sharing options...
freelance84 Posted November 21, 2011 Share Posted November 21, 2011 replace $array with what ever its real name is... $totPrice = $array['ASDA']['Meat']['Chicken'] + $array['ASDA']['Meat']['Beef']; That should work i think. Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290048 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 echo implode(" + ",array_keys($list['ASDA']['Meat'])).' = ' . array_sum($list['ASDA']['Meat']); (assuming main array is called $list) Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290051 Share on other sites More sharing options...
Pikachu2000 Posted November 21, 2011 Share Posted November 21, 2011 Assuming you can't be sure that the only meats will be 'chicken' and 'beef', and want the total of all the meats . . . $meat_total = array_sum( $array['ASDA']['Meat'] ); Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290053 Share on other sites More sharing options...
SeanHarding Posted November 21, 2011 Author Share Posted November 21, 2011 Worked perfectly. Is there a way to automate it? If I had numbered keys I'd use $i++ to run through them. Basically I need a total under my Veg and Meat section. If I'm updating the prices individually I'd like code to update the total. And how would I add Pork? lol Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290055 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 (you should read a bit about arrays) $array['ASDA']['Meat']['Pork'] = 45; Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290056 Share on other sites More sharing options...
SeanHarding Posted November 21, 2011 Author Share Posted November 21, 2011 Have read quite allot about arrays but the multidimensional ones still confuse me. How about removing Cabbage completely? Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290057 Share on other sites More sharing options...
freelance84 Posted November 21, 2011 Share Posted November 21, 2011 $array[$supermarket][$foodArea][$food]; Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290061 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 Multidemensional arrays are THE SAME as normal arrays except each element can be another array... remove cabbage: unset($array['ASDA']['Veg']['Cabbage']); Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290062 Share on other sites More sharing options...
SeanHarding Posted November 21, 2011 Author Share Posted November 21, 2011 Thanks Guys, I have been stuck on this for a while. Reading your reply I can see that I set the item as a Key and all I need is the Value. Thanks for your help. I may be back Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290067 Share on other sites More sharing options...
SeanHarding Posted November 21, 2011 Author Share Posted November 21, 2011 ooooo how would I add Pork? Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290070 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 I've already answered that one. Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290071 Share on other sites More sharing options...
SeanHarding Posted November 21, 2011 Author Share Posted November 21, 2011 I love you, Cheers Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290073 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 lol, we love you too man! Link to comment https://forums.phpfreaks.com/topic/251552-multidimensional-array-manipulation/#findComment-1290083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.