phplaravellaravel-excel

How to convert a array value one column to multiple columns in laravel excel?


Here I attached my recent Laravel-excel image. Here I mark $items_name column. Which I want to need in multiple columns. For that reason I use foreach loop, and implode and also array_map. [1]: https://i.sstatic.net/PaMM3.jpg

I want to show $item_name values in different columns not in a single column.

if($customer_type == 2)
    {
        $excel_data_attribute = array(
            array('Distributor Customer')
        );

        if ($show_type == 0)
        {
            $excel_data_attribute_array = array(
                array('Sales Sheet - Quanty Wise')
            );
        }elseif($show_type == 1)
        {
            $excel_data_attribute_array = array(
                array('Sales Sheet - Price Wise')
            );
        }else
        {
            $excel_data_attribute_array = array(
                array('Sales Sheet - Cartoon Quanty Wise')
            );
        }
        $excel_dynamic_data_array = array();
        $index=0;
        foreach ($productGroup as $gKey => $group )
        {
            $col_[$gKey] = false;
            foreach($all_product as $pkey => $product)
            {
                if($productGroup[$gKey]["id"] == $product->product_group_id)
                {
                    $col_[$gKey] = true;
                }
            }
            if($col_[$gKey])
            {
                $product_Names_with_keys = $productGroup[$gKey]["t_product"].$productGroup[$gKey]['gorupName'];

                $excel_dynamic_data_array[$index][] = $productGroup[$gKey]['gorupName'];
                $index++;
            }
        }
        $item  = $excel_dynamic_data_array;

        $item_name = implode(', ', array_map(function ($entry) {
                                                return ($entry[key($entry)]);
                                            }, $item));
        if($summery == 1)
        {
            $excel_data_attribute_array_column = array(
                array('SL','Region & Rsm',$item_name,'Total')
            );
        }else
        {
            if ($customer_type == 2)
            {
                $excel_data_attribute_array_column = array(
                    array('SL','Order No','Order Date','Challan No','Challan Date','Bill No','Party Name','Area',$item_name,'Total')
                );

            }else
            {
                $excel_data_attribute_array_column = array(
                    array('SL','Order No','Order Date','Bill No','Party Name','Area',$item_name,'Total')
                );
            }
        }
        $total_final_array = array_merge($excel_data_attribute,$excel_data_attribute_array,$excel_data_attribute_array_column);
    }
   return collect($total_final_array); 

Solution

  • $excel_data_attribute_array_column = array(
          array_merge(
            array('SL','Order No','Order Date','Challan No','Challan Date','Bill No','Party Name','Area'),
            explode(',',$item_name),
            array('Total')
          )
     );
    

    Correct ans is-