Jump to content

Array Help


Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/264440-array-help/
Share on other sites

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;
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/264440-array-help/#findComment-1355147
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.