I'm not familiar with PHP associate arrays so I'm hoping someone can shed some light on the subject and suggest how to solve my particular problem.

I have an array of data, of which each element is a string of "Month, Year". I want to parse through that data and create an associate array where the keys are the years and the values are an array of months of that year.

For example, I have the array("November, 2011", "May, 2011", "July, 2010") Using a foreach loop, I want to parse through this data and create the array:

array( "2011" => array("Novemeber", "May"), "2010" => array("July"))

From what I can see, I need to know how to check if a key is there, if it isn't create it and make a new array as its value, if it is, append the month to the already there value array.

If that makes sense, any help would be appreciated! thanks!

share|improve this question
"associative" arrays, just to be nit-picky. – Marc B Nov 18 '11 at 21:39

4 Answers

up vote 2 down vote accepted
$arr = array("November, 2011", "May, 2011", "July, 2010");

// An array to hold the output
$outarr = array();

foreach ($arr as $pair) {
  // Split the month/year from each pair
  list($mon, $year) = explode(",", $pair);
  // Trim whitespace on the $year
  $year = trim($year);  

  // If the year key isn't set, create it now
  if (!isset($outarr[$year])) $outarr[$year] = array();

  // And append the month. Don't forget to trim whitespace!
  $outarr[$year][] = trim($mon);
}

The output:

print_r($outarr);
Array
(
    [ 2011] => Array
        (
            [0] => November
            [1] => May
        )

    [ 2010] => Array
        (
            [0] => July
        )  
)
share|improve this answer
Whitespace should be trimmed at least around year (not month) I think. – Frosty Z Nov 18 '11 at 21:49
1  
@FrostyZ oh yes, you're correct. added above. – Michael Berkowski Nov 18 '11 at 21:49
<?php

$initialArray = array("November, 2011", "May, 2011", "July, 2010");

$finalArray = array();

foreach($initialArray as $value)
{
  // Note: I suppose here that each initialArray element is "<Month>, <Year>",
  // otherwise the following line may trigger errors.
  list($month, $year) = array_map('trim', explode(",", $value));

  if (!isset($finalArray[$year]))
    $finalArray[$year] = array();

  $finalArray[$year][] = $month;
}

// orders years, however months arrays below years won't be ordered.
ksort($finalArray); 
share|improve this answer

Try this (Edit 2nd pass, forgot keyword array, added trim):

$array = array("November, 2011", "May, 2011", "July, 2010");

foreach ($array as $element) {

    list($month, $year) = explode(',', $element);

    $years[trim($year)][] = trim($month);
}
share|improve this answer
Syntax error on your first line + a trim() should be applied to year keys – Frosty Z Nov 18 '11 at 21:52
1  
@FrostyZ: haha thanks. Forgot keyword array, it's what I get for doing it without testing. – Mike Purcell Nov 18 '11 at 21:57

Alright, let's try this:

<?php

$my_array = array('November, 2011', 'May, 2011', 'July, 2010');
$final = array();

foreach($my_array as $item) {

    // Let's split the Month and Year
    $item = explode(', ', $item);
    $final[$item[1]][] = $item[0];
}

print_r($final);

?>

The final output should be:

Array
(
    [2011] => Array
        (
            [0] => November
            [1] => May
        )

    [2010] => Array
        (
            [0] => July
        )

)
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.