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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.