1

I have a *.txt file like this:

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   

As you can see, Each line is started by unique code. And basically those file is parent-child format as

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

Step 1, I convert them into an array based the file name of those txt.

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

The result like this:

[
    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'
]

My goal is, I want to separated them into sub array based the 5 beginning character of each line.

If the string is = 'DTL01', I want to create them into one sub array. Like this

[
    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....
]

As you can see, there ara two "DTL01", so the array must have two elements. Please advise.

So far,

$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*/
    }
}

Result:

[
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' => []
]
5
  • Is your array result always contain same amount of charater as in DTL0101I00010000 and DTL0101I00020000? Commented Nov 19, 2018 at 5:21
  • No, its increment , something like DTL0101I00060000, DTL0101I00070000 and so on Commented Nov 19, 2018 at 5:23
  • Okay but it's same like before | it contain DTL0101 and after that is has 00010000 and 00020000 and so on right? Commented Nov 19, 2018 at 5:24
  • Ya, each DTL01 is a subArray, do not care about the character afterwards Commented Nov 19, 2018 at 5:26
  • What defines what is a parent and what is a child? i.e. how do you know DTL02 and CNT01 are children of DTL01 and not just parents themselves? Commented Nov 19, 2018 at 6:17

1 Answer 1

3

Hope this will help you.

$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;
    }
}
Sign up to request clarification or add additional context in comments.

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.