1

This is my array:

$arr = array(
  0 => array(
      'title' => 'test1',
      'count' => 4,
      'month' => 'jan-2015'
    ),
  1 => array(
      'title' => 'test2',
      'count' => 10,
      'month' => 'jan-2015'
    ),
  2 => array(
      'title' => 'test3',
      'count' => 14,
      'month' => 'jun-2015'
    ),
  3 => array(
      'title' => 'test4',
      'count' => 45,
      'month' => 'july-2015'
    ),
    4 => array(
      'title' => 'test1',
      'count' => 40,
      'month' => 'jun-2015'
    ),
);

I've to convert this array as below:

 $arr = array(
  'jan-2015' => array(
      0 => array(
        'title' => 'test1',
        'count' => 4,
      ),
      1 => array(
        'title' => 'test2',
        'count' => 10,
      ),
      2 => array(
        'title' => 'test3',
        'count' => 0,
      ),
      3 => array(
        'title' => 'test4',
        'count' => 0,
      ),
    ),
  'jun-2015' => array(
      0 => array(
        'title' => 'test1',
        'count' => 40,
      ),
      1 => array(
        'title' => 'test2',
        'count' => 0,
      ),
      2 => array(
        'title' => 'test3',
        'count' => 14,
      ),
      3 => array(
        'title' => 'test4',
        'count' => 0,
      ),
   ),
  'july-2015' => array(
      0 => array(
        'title' => 'test1',
        'count' => 0,
      ),
      1 => array(
        'title' => 'test2',
        'count' => 0,
      ),
      2 => array(
        'title' => 'test3',
        'count' => 0,
      ),
      3 => array(
        'title' => 'test4',
        'count' => 45,
     ),
   ),
);

That is if there is no title for particular month, then i need to add that title and count will be 0. how to do this? I've tried this concept:

$gen_arr = array_fill_keys(array_column($arr, 'month'), array());
$final_arr = array(); // Array to store the result
foreach ($gen_arr as $gen_key => $gen_value) {
  foreach ($arr as $org_key => $org_value) {
    //temporarily store the original array
    $temp = $org_value;
    if ($gen_key != $temp['month'] && !in_array($temp['title'], $final_arr[$gen_key])) {
      $temp['month'] = $gen_key;
      $temp['count'] = 0;
    }

    $final_arr[$gen_key][] = $temp;
  }
}
return $final_arr;

But i can't get what i wanted. Is any other solutions for this?

6
  • You mean you want all 12 months of the year to be the keys of array? Even the month does not exist in the input array, it should appear in output array like 'feb-2015 => array()' ? Commented Jan 8, 2016 at 8:16
  • not like that... Have to add title value only if there is no title in particular 'mon-2016'
    – Guru
    Commented Jan 8, 2016 at 8:18
  • titles are dynamic right? it could go to test10000..?
    – roullie
    Commented Jan 8, 2016 at 8:31
  • can you give an example of the final array you want generated? I am having a hard time understanding what you are asking
    – Clay
    Commented Jan 8, 2016 at 8:34
  • @Clayton final array is already on the question
    – roullie
    Commented Jan 8, 2016 at 8:34

2 Answers 2

0

You can try this code:

$arr = array(
    0 => array(
        'title' => 'test1',
        'count' => 4,
        'month' => 'jan-2015'
    ),
    1 => array(
        'title' => 'test2',
        'count' => 10,
        'month' => 'jan-2015'
    ),
    2 => array(
        'title' => 'test3',
        'count' => 14,
        'month' => 'jun-2015'
    ),
    3 => array(
        'title' => 'test4',
        'count' => 45,
        'month' => 'july-2015'
    ),
    4 => array(
        'title' => 'test1',
        'count' => 40,
        'month' => 'jun-2015'
    ),
);

$titles = array_unique( array_column( $arr, 'title' ) );
$months = array_unique( array_column( $arr, 'month' ) );

// Construct an array with the correct structure but with zero counts
$resultingArray = [];
foreach( $months as $month ){
    if( !is_array( $resultingArray[$month] ) ){
        $resultingArray[ $month ] = [];
    }
    foreach( $titles as $title ){
        $resultingArray[ $month ][] = ['title' => $title, 'count' => 0, 'month' => $month ];
    }
}
// And now populate the counts
foreach( $arr as $a ){
    foreach( $resultingArray as $month => &$tempArray ){
        foreach( $tempArray as &$array ){
            if( $a['month'] === $month && $a['title'] === $array['title'] ){
                $array['count'] = $a['count'];
            }
        }
    }
}

echo '<pre>';
print_r( $resultingArray );
echo '</pre>';
8
  • @Guru The output is exactly as the one you posted. May be you could explain in more details what exactly you need?
    – mrun
    Commented Jan 8, 2016 at 8:55
  • Great! In that case can you please retract the downvote?
    – mrun
    Commented Jan 8, 2016 at 9:04
  • I need also month in that array
    – Guru
    Commented Jan 8, 2016 at 9:12
  • It have some error. Please look title "test1 test1" for jun-2015. It have count 40. but it code gives 0.
    – Guru
    Commented Jan 8, 2016 at 9:18
  • I literally just copy/pasted the code from my answer and it displays 40 for title test1 for jun-2015
    – mrun
    Commented Jan 8, 2016 at 9:22
0

Please, try this code for your expectation.

$new_array = array();
foreach($arr as $each_arr){
    $month = $each_arr['month'];
    $new_array[$month][] = array(
        'title' => $each_arr['title'],
        'count' => $each_arr['count'],
    );
}

var_dump($new_array);

echo '<pre>';
print_r($new_array);
echo '</pre>';
1
  • 1
    This doesn't produce the desired array. Please read the requirements carefully!
    – mrun
    Commented Jan 8, 2016 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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