将文本文件解析为数组,然后根据字符串的一部分进行操作

时间:2018-11-19 05:03:38

标签: php arrays

我有一个* .txt文件,如下所示:

DTL0101I00010000-- IRBNDIRBNDIDTPPIDTPPINTERASIA PROGRESS                                0548511591                    20180505                              20180529  000000001021100000000000000000000000000050000010000000000000000000000000000PX
DTL02SNM  SINOASIAN TRADING INTERNATIONAL LTD.
DTL02SNA  OLIAJI TRADE CENTRE, 1ST FLOOR, VICTORIA MAHE, SEYCHELLES TEL: +248 4321 377
DTL02CNM  PT ELANGPERDANA TYRE INDUSTRY
DTL02CNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA TEL:+62(21)8765105
DTL02NNM  PT ELANGPERDANA TYRE INDUSTRY
DTL02NNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA TEL:+62(21)8765105
DTL02SMR  NIL 
DTL02HSC  2803
DTL02DES  CARBON BLACK N330 PO NO. 18/04/PO/02547 DATED 4/26/2018 CFR JAKARTA, INDONESIA HS CODE:28030010 
CNT010001TCNU9770332              40F  WHLB639733     
CNT010001WHLU5405867              40F  WHLB639731     
CNT010001WHLU5578013              40F  WHLB639732     
CNT010001WHSU5102911              40F  WHLB639735     
CNT010001WHSU5452632              40F  WHLB639734     
DTL0101I00020000-- IRBNDIRBNDIDTPPIDTPPINTERASIA PROGRESS                                0548511603                    20180505                              20180529  000000000822600000000000000000000000000040000008000000000000000000000000000PX
DTL02SNM  SINOASIAN TRADING INTERNATIONAL LTD.
DTL02SNA  OLIAJI TRADE CENTRE, 1ST FLOOR, VICTORIA MAHE, SEYCHELLES TEL: +248 4321 377
DTL02CNM  PT ELANGPERDANA TYRE INDUSTRY
DTL02CNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA
DTL02NNM  PT ELANGPERDANA TYRE INDUSTRY
DTL02NNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA
DTL02SMR  NIL 
DTL02HSC  2803
DTL02DES  80,000.00 KGS OF CARBON BLACK N330 CFR JAKARTA, INDONESIA PO NO. 18/04/PO/02547 DATED4/26/2018 HS CODE:28030010 

CNT010002TCKU9697617              40F  WHLB639827     
CNT010002TCNU4338347              40F  WHLB639829     
CNT010002WHLU5419958              40F  WHLB639826     
CNT010002WHLU5782449              40F  WHLB639828   

如您所见, 每行以唯一的代码开头。 基本上这些文件是父子格式,

parent is 'DTL01'
child is 'DTL02', 'CNT01'

第1步,我根据这些txt的文件名将它们转换为数组。

$file = new \SplFileObject($basePath . $this->master->baseName . '.' . $this->master->extension);

结果如下:

[
    0 => 'DTL0101I00010000-- IRBNDIRBNDIDTPPIDTPPINTERASIA PROGRESS                                0548511591                    20180505                              20180529  000000001021100000000000000000000000000050000010000000000000000000000000000PX'
    1 => 'DTL02SNM  SINOASIAN TRADING INTERNATIONAL LTD.'
    2 => 'DTL02SNA  OLIAJI TRADE CENTRE, 1ST FLOOR, VICTORIA MAHE, SEYCHELLES TEL: +248 4321 377'
    3 => 'DTL02CNM  PT ELANGPERDANA TYRE INDUSTRY'
    4 => 'DTL02CNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA TEL:+62(21)8765105'
    5 => 'DTL02NNM  PT ELANGPERDANA TYRE INDUSTRY'
    6 => 'DTL02NNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA TEL:+62(21)8765105'
    7 => 'DTL02SMR  NIL'
    8 => 'DTL02HSC  2803'
    9 => 'DTL02DES  CARBON BLACK N330 PO NO. 18/04/PO/02547 DATED 4/26/2018 CFR JAKARTA, INDONESIA HS CODE:28030010'
    10 => 'CNT010001TCNU9770332              40F  WHLB639733'
    11 => 'CNT010001WHLU5405867              40F  WHLB639731'
    12 => 'CNT010001WHLU5578013              40F  WHLB639732'
    13 => 'CNT010001WHSU5102911              40F  WHLB639735'
    14 => 'CNT010001WHSU5452632              40F  WHLB639734'
    15 => 'DTL0101I00020000-- IRBNDIRBNDIDTPPIDTPPINTERASIA PROGRESS                                0548511603                    20180505                              20180529  000000000822600000000000000000000000000040000008000000000000000000000000000PX'
    16 => 'DTL02SNM  SINOASIAN TRADING INTERNATIONAL LTD.'
    17 => 'DTL02SNA  OLIAJI TRADE CENTRE, 1ST FLOOR, VICTORIA MAHE, SEYCHELLES TEL: +248 4321 377'
    18 => 'DTL02CNM  PT ELANGPERDANA TYRE INDUSTRY'
    19 => 'DTL02CNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA'
    20 => 'DTL02NNM  PT ELANGPERDANA TYRE INDUSTRY'
    21 => 'DTL02NNA  NPWP:016386112436000 JALAN ELANG DESA SUKAHATI CITEUREUP BOGOR 16810, INDONESIA'
    22 => 'DTL02SMR  NIL'
    23 => 'DTL02HSC  2803'
    24 => 'DTL02DES  80,000.00 KGS OF CARBON BLACK N330 CFR JAKARTA, INDONESIA PO NO. 18/04/PO/02547 DATED4/26/2018 HS CODE:28030010'
    25 => 'CNT010002TCKU9697617              40F  WHLB639827'
    26 => 'CNT010002TCNU4338347              40F  WHLB639829'
    27 => 'CNT010002WHLU5419958              40F  WHLB639826'
    28 => 'CNT010002WHLU5782449              40F  WHLB639828'
]

我的目标是,我想根据每行的5个开始字符将它们分成子数组。

如果字符串= = 'DTL01',我想将它们创建为一个子数组。像这样

[
    0 => [
        'parent' => [
            'id' => {generateSomeUniqueTokenCode},
            'value' => $valueOf-DTL01
        ],
        'child' => [
            'parent_id' => $parent_id_above
            'DTL02' => [
                '0' => [
                    'id' => {generateSomeUniqueTokenCode},
                    'value' => $valueOf-first-DTL02
                ],
                '1' => [
                    'id' => {generateSomeUniqueTokenCode},
                    'value' => $valueOf-second-DTL02
                ],
                ,
                so on ...
            ],
            'CNT01' =>[
                '0' => [
                    'id' => {generateSomeUniqueTokenCode},
                    'value' => $valueOf-first-CNT01
                ],
                '1' => [
                    'id' => {generateSomeUniqueTokenCode},
                    'value' => $valueOf-first-CNT01
                ],
            ]
        ],
    ],

    so on for the next element....
]

如您所见,这里有两个“ DTL01”,因此数组必须有两个元素。 请告知。

到目前为止,

$tempArray = [];
$parentId = 1;
foreach ($array as $key => $sentences) {

    /** Get 5 Characters as a key * */
    $keyword = substr($sentences, 0, 5);

    /*
     * Create a variable to decide where is the parent
     * When found 'DTL01', create new subArray
     * */
    $index = ($keyword == 'DTL01') ? TRUE : FALSE;

    if ($index) {
        $tempArray[] = [
            'parent' => [
                'id' => str_pad($parentId, 20, '0', STR_PAD_LEFT),
                'value' => $sentences
            ],
            'child' => []
        ];
        $parentId++;
    } else {
        /*Still stuck*/
    }
}

结果:

[
0 => [
    'parent' => [
        'id' => '00000000000000000001'
        'value' => 'DTL0101I00010000-- IRBNDIRBNDIDTPPIDTPPINTERASIA PROGRESS                                0548511591                    20180505                              20180529  000000001021100000000000000000000000000050000010000000000000000000000000000PX'
    ]
    'child' => []
]
1 => [
    'parent' => [
        'id' => '00000000000000000002'
        'value' => 'DTL0101I00020000-- IRBNDIRBNDIDTPPIDTPPINTERASIA PROGRESS                                0548511603                    20180505                              20180529  000000000822600000000000000000000000000040000008000000000000000000000000000PX'
    ]
    'child' => []
]

1 个答案:

答案 0 :(得分:3)

希望这会对您有所帮助。

$tempArray = [];
$parentId = 1;
$counter = 0;
foreach ($array as $key => $sentences) {
    /*$tempSentences = '';
    $tempSentences = $sentences;*/
    //echo $sentences;echo '<br>';

    /** Get 5 Characters as a key * */
    $keyword = substr($sentences, 0, 5);

    /*
     * Create a variable to decide where is the parent
     * When found 'DTL01', create new subArray
     * */
    $index = ($keyword == 'DTL01') ? TRUE : FALSE;

    if ($index) {
        $tempArray[] = [
            'parent' => [
                'id' => str_pad($parentId, 20, '0', STR_PAD_LEFT),
                'value' => $sentences
            ],
            'child' => []
        ];
        if($parentId !=1){
            $counter++;
        }
        $parentId++;
    } else {
            $tempArray[$counter]['child'][] = $sentences;
    }
}