php - How to generate array from an array

149

I have an array but I want to generate other array from that one..

    Array
    (
        [0] => Array
            (
                [supplier] => Billy 
                [total_bookings] => 5
                [year] => 2016
                [month] => 6
                [user_id] => 4
                [sales_revenue] => 1180
                [net_revenue] => 1180
            )

    [1] => Array
        (
            [supplier] => XYZ1
            [total_bookings] => 3
            [year] => 2016
            [month] => 6
            [user_id] => 2
            [sales_revenue] => 642
            [net_revenue] => 642
        )

    [2] => Array
        (
            [supplier] => Billy 
            [total_bookings] => 1
            [year] => 2016
            [month] => 3
            [user_id] => 4
            [sales_revenue] => 30
            [net_revenue] => 30
        )

    [3] => Array
        (
            [supplier] => Billy 
            [total_bookings] => 1
            [year] => 2015
            [month] => 10
            [user_id] => 4
            [sales_revenue] => 30
            [net_revenue] => 30
        )

)

to new array :

      Array
    (
        [2016] => Array(
     [6] => Array
        (

           [0] => Array( 

            [supplier] => Billy 
            [total_bookings] => 5
            [user_id] => 4
            [sales_revenue] => 1180
            [net_revenue] => 1180
          ) 

          [1] => Array
            (
                [supplier] => XYZ1
                [total_bookings] => 3
                [user_id] => 2
                [sales_revenue] => 642
                [net_revenue] => 642
            )

        )

       [3] => Array
        (

           [0] => Array
            (
                [supplier] => Billy 
                [total_bookings] => 1
                [year] => 2016
                [month] => 3
                [user_id] => 4
                [sales_revenue] => 30
                [net_revenue] => 30
            )


        )  


    )

     [2015] => Array(

        [10] => Array
        (
            [supplier] => Billy 
            [total_bookings] => 1
            [user_id] => 4
            [sales_revenue] => 30
            [net_revenue] => 30
        )

     )

)
804

Answer

Solution:

The solution usingarray_fill_keys,array_column(available since PHP 5.5),array_walk andarray_diff_key functions:

// supposing $arr is your initial array
$years = array_fill_keys(array_column($arr, 'year'), []);
array_walk($arr, function($v) use(&$years){
    if (key_exists($v['year'], $years)) {
        $years[$v['year']][$v['month']][] = array_diff_key($v, ['year'=>0, 'month'=>0]);
    }
});
print_r($years);
467

Answer

Solution:

Try this one and let me know:

$new_arr = array();
foreach($arr as $val){
    $temp = array('supplier' => $val['supplier'], 'total_bookings' => $val['total_bookings'], 'user_id' => $val['user_id'], 'sales_revenue' => $val['sales_revenue'], 'net_revenue' => $val['net_revenue']);
    array_push($new_arr[$val['year']][$val['month']], $temp);
}
print_r($new_arr);
484

Answer

Solution:

Try this one:-

$res = [];
foreach($array as $record){
  $year = $record['year'];
  $month = $record['month'];
  unset($record['year'],$record['month']);
  $res[$year][$month][] = $record;
}
echo '<pre>'; print_r($res);

People are also looking for solutions to the problem: PHP Combine Array and Sorting It

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.