0

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?

3
  • Using on loop you can do it, you can convert your array into new array using the loop. Commented Jan 7, 2016 at 12:25
  • can u provide sample code? Commented Jan 7, 2016 at 12:26
  • Show us what you have tried. SO isn't a free coding service. Commented Jan 7, 2016 at 12:28

4 Answers 4

2

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

2

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
                )
        )
)

2 Comments

Can you explain why this is not correct? It gives the output you specified in your question? Or am I missing something?.
Note that my output matches your specified output, while the accepted answer does not (note the presence of the "month" key).
1

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;

1 Comment

@Guru what you mean by count is different. compare my output with your output give.
1

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);
?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.