首页 > 解决方案 > CSV 文件中的动态内容

问题描述

我在 PHP 中创建 CSV 文件,它的工作很完美,但问题是某些数据是动态依赖于 csv 中的其他列的。

例如:

在此处输入图像描述

每个老师都有动态的科目和学生,我想这样显示数据。

以编程方式,我有 3 个不同数组中的数据(不包括标题)。我的代码运行良好,但对于第一部分和第二部分。

无法合并与第一部分和第二部分平行的第三个阵列。

代码和示例图像映射是,

Part One = $leads[]
Part Two = $calls[]
Part Three = $meetings[]

我的代码是:

 <?php       if (count($leads)) {

        foreach ($leads as $k) {

            //merging lead with every call 
            foreach($calls as $key => $t){
                $merg[$key] = array_merge($k, $t);
            }
            foreach($merg as $entry){
                fputcsv($file, $entry, ",", " ");
            }

        }
    }

如何以它们的工作方式合并数组。

对于第一个数组的每个元素,第一个和第二个数组将动态显示以获得上述结果?

$lead、$calls、$meetings 的结构分别是,

    Array
  (
   [0] => Array
    (
        [leads_id] => 226740
        [created_date] => 2018-06-06 00:00:00
        [lead_name] => My Lead
        [organization] => Orgnization
        [address] => Street No.05
        [contact_name] => Contact Name
        [email] => Email@email.com
        [phone] => 1234567989
        [mobile] => 123456789
        [facebook] => http://www.facebook.com/myfb
        [skype] => Skype@skype.com
        [twitter] => http://www.twitter.com/abc
        [lead_desc] => This is Short Note.
        [status] => Open
    )

 )

 ------------------------
Array
 (
[0] => Array
    (
        [calls_id] => 79
        [date] => 0000-00-00
        [time] => 01:10PM
        [call_summary] => This is Summary of the call
    )

[1] => Array
    (
        [calls_id] => 80
        [date] => 0000-00-00
        [time] => 03:15PM
        [call_summary] => This is Summary  of the call2
    )

[2] => Array
    (
        [calls_id] => 81
        [date] => 0000-00-00
        [time] => 01:05PM
        [call_summary] => This is Summary  of the call3
    )

 )

 -------------------

 Array
(
[0] => Array
    (
        [mettings_id] => 36
        [meeting_subject] => Gujranwala
        [start_date] => 0000-00-00
        [end_date] => 0000-00-00
        [start_time] => 02:10PM
        [end_time] => 01:00PM
        [location] => SIalkot
        [description] => Hello  World
    )

[1] => Array
    (
        [mettings_id] => 37
        [meeting_subject] => Introduction
        [start_date] => 0000-00-00
        [end_date] => 0000-00-00
        [start_time] => 11:55PM
        [end_time] => 03:15PM
        [location] => Lahore
        [description] => this is  description
    )

 )

标签: phparrayslaravelcsvmerge

解决方案


我需要您的输入数据数组($leads、$calls、$meetings)的确切结构来帮助您,我可能不会使用 array_merge(),具体取决于结构(仅循环)。


推荐阅读