Guru
Guru

Reputation: 420

PHP - generate multi-dimentional array

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'
    ),
);

I've to convert this array into multi-dimentional array as below:

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

I've tried to make it as expected but unfortunately i can't make this. Is any other solutions for this?

Upvotes: 0

Views: 106

Answers (4)

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

According to your data structure :

$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'
    ),
);

try this:

$newArray = array();
foreach($arr as $key => $val) {
    $newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';

Output:

Array
(
    [jan-2015] => Array
        (
            [0] => Array
                (
                    [title] => test1
                    [count] => 4
                    [month] => jan-2015
                )

            [1] => Array
                (
                    [title] => test2
                    [count] => 10
                    [month] => jan-2015
                )

        )

    [jun-2015] => Array
        (
            [0] => Array
                (
                    [title] => test3
                    [count] => 14
                    [month] => jun-2015
                )

        )

    [july-2015] => Array
        (
            [0] => Array
                (
                    [title] => test4
                    [count] => 45
                    [month] => july-2015
                )

        )

)

Upvotes: 2

trincot
trincot

Reputation: 350310

You could use this function:

function transform($input) {
    // Extract months, and use them as keys, with value set to empty array
    // The array_fill_keys also removes duilicates
    $output = array_fill_keys(array_column($input, 'month'), array());

    foreach ($input as $element) {
        $copy = $element;
        // remove the month key
        unset($copy["month"]);
        // assign this to the month key in the output 
        $output[$element["month"]][] = $copy;
    }
    return $output;
}

Call it like this:

$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'
    ),
);

print_r (transform($arr));

Output:

Array
(
    [jan-2015] => Array
        (
            [0] => Array
                (
                    [title] => test1
                    [count] => 4
                )
            [1] => Array
                (
                    [title] => test2
                    [count] => 10
                )
        )
    [jun-2015] => Array
        (
            [0] => Array
                (
                    [title] => test3
                    [count] => 14
                )
        )
    [july-2015] => Array
        (
            [0] => Array
                (
                    [title] => test4
                    [count] => 45
                )
        )
)

Upvotes: 2

Narayan Bhandari
Narayan Bhandari

Reputation: 426

There could many way to do this. Please try this one if it works for you

<?php
$newArr=NULL;
foreach($arr as $array)
{
    $temp=NULL;
    $temp['title']=$array['title'];
    $temp['count']=$array['count'];

    $newArr[$array['month']][]=$temp;
}

var_dump($newArr);
?>

Upvotes: 1

Maha Dev
Maha Dev

Reputation: 3965

By using answer of @Girish Patidar, You can achieve this by:

$outputArr = array();
$to_skip = array();

foreach($arr as $row){
  $to_skip = $row;
  unset($to_skip['month']);
  $outputArr[$row['month']][] = $to_skip;
}

echo "<pre>";
print_r($outputArr);
die;

Upvotes: 1

Related Questions