Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining array question

Status
Not open for further replies.

DreamerZ

Programmer
Jul 11, 2001
254
US
I have an multidimensional array where if 3 values are the same, I want to combine them and add other values together. Here's the array:
Code:
Array
(
    [0] => Array
        (
            [date] => 1127977200
            [strDate] => 09/29/2005
            [shift] => Day
            [title] => COMPUTER_OPERATOR
            [slots] => 1
        )

    [1] => Array
        (
            [date] => 1127977200
            [strDate] => 09/29/2005
            [shift] => Day
            [title] => COMPUTER_OPERATOR
            [slots] => 1
        )

    [2] => Array
        (
            [date] => 1127977200
            [strDate] => 09/29/2005
            [shift] => Day
            [title] => COMPUTER_OPERATOR
            [slots] => 1
        )

    [3] => Array
        (
            [date] => 1127977200
            [strDate] => 09/29/2005
            [shift] => Day
            [title] => SSA
            [slots] => 1
        )

    [4] => Array
        (
            [date] => 1128063600
            [strDate] => 09/30/2005
            [shift] => Day
            [title] => SSA
            [slots] => 1
        )

)

As you can see, the first 3 entries have the same date, shift, and title. I combine those into a new array and add each slots value together. So, the above array would combine to:
Code:
Array
(
    [0] => Array
        (
            [date] => 1127977200
            [strDate] => 09/29/2005
            [shift] => Day
            [title] => COMPUTER_OPERATOR
            [slots] => 3
        )

    [1] => Array
        (
            [date] => 1127977200
            [strDate] => 09/29/2005
            [shift] => Day
            [title] => SSA
            [slots] => 1
        )

    [2] => Array
        (
            [date] => 1128063600
            [strDate] => 09/30/2005
            [shift] => Day
            [title] => SSA
            [slots] => 1
        )

)

Can't for the life of me, figure out how to do that. Thoughts? TIA


DreamerZ
 
I knew if I played long enough I'd figure it out. Not elegant, but it works.

Went through the full array and combined the values I wanted into a new array. Then, for each key in that new array, I went through the original array and added the slots value together. Finally, outside the full array, I put all the values into the new, combined array. Here's the code if you're interested:
Code:
  $tmpSlots = 0;
  for ($a=0; $a<count($arrayDates); $a++)
  {
    for ($i=0; $i<count($arrayOpenTmp); $i++)
    {
	if ($arrayOpenTmp[$i]["date"] == $arrayDates[$a]["date"] && $arrayOpenTmp[$i]["shift"] == $arrayDates[$a]["shift"] && 
	  $arrayOpenTmp[$i]["title"] == $arrayDates[$a]["title"])
	{
	  $tmpSlots = $tmpSlots + $arrayOpenTmp[$i]["slots"];
	}
    }
    $arrayOpen[] = array("date"=>$arrayDates[$a]["date"], "strDate"=>date("m/d/Y", $arrayDates[$a]["date"]), 
    "shift"=>$arrayDates[$a]["shift"], "title"=>$arrayDates[$a]["title"], "slots"=>$tmpSlots);
    $tmpSlots = 0;
  }

If anyone has a more efficient way to do this, please let me know.


DreamerZ
 
How's this? It uses serialize(), unserialize() and array_unique(). The $orig array is the one you started with, $new_orig is the array with unique entries. The echo statement are for debugging, so you can see what's going on.
Code:
echo '<pre>';print_r($orig);echo '</pre>';
foreach ($orig as $k=>$na)
	$new[$k] = serialize($na);
echo '<pre>';print_r($new);echo '</pre>';
$uniq = array_unique($new);
echo '<pre>';print_r($uniq);echo '</pre>';
$i = 0;
foreach($uniq as $ser)
	$new_orig[$i++] = unserialize($ser);
echo '<pre>';print_r($new_orig);echo '</pre>';

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top