raymond_feliciano Posted June 19, 2012 Share Posted June 19, 2012 I have an array which is created dynamically here is the output Advair=>40mg Advair=>50mg Dulera=>50mcg Flovent=>20mcg Symbicort=>120/4.5 Symbicort=>140/4.5 As you can see it has some repeated data. I want to know if there is a way to consolidate this array so I can display it like this Advair=>40mg, 50mg Dulera=>50mcg Flovent=>20mcg Symbicort=>120/4.5, 140/4.5 I just want to keep all dosages which belong to the same prescription together. I tried using array_unique() and other methods with no luck. any help is welcomed thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/264440-array-help/ Share on other sites More sharing options...
scootstah Posted June 19, 2012 Share Posted June 19, 2012 This may not be the prettiest solution but it works: $array = array( array('Advair' => '40mg'), array('Advair' => '50mg'), array('Dulera' => '50mcg'), array('Flovent' => '20mcg'), array('Symbicort' => '120/4.5'), array('Symbicort' => '140/4.5') ); $new_array = array(); foreach ($array as $arr) { foreach($arr as $key => $val) { if (array_key_exists($key, $new_array)) { $new_array[$key] .= ', ' . $val; } else { $new_array[$key] = $val; } } } Quote Link to comment https://forums.phpfreaks.com/topic/264440-array-help/#findComment-1355147 Share on other sites More sharing options...
raymond_feliciano Posted June 19, 2012 Author Share Posted June 19, 2012 Thanks I was working on a solution similar to your but would work the way I need it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/264440-array-help/#findComment-1355153 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.