0

I'm trying to figure out what I'm doing that gives me the wrong result.

I have a file composed this way:

2017/05/02  BA  86  23  39  89  5
2017/05/02  CA  23  4   14  86  5
2017/05/02  FI  71  26  19  15  6
2017/05/02  GE  69  5   16  58  83
2017/05/02  MI  19  45  4   16  68
2017/05/02  NA  6   16  75  78  89
2017/05/02  PA  67  83  21  5   1
2017/05/02  RM  51  79  12  47  19
2017/05/02  RN  27  83  41  37  23
2017/05/02  TO  44  17  60  31  53
2017/05/02  VE  89  32  48  71  57
2017/05/04  BA  61  70  47  71  75
2017/05/04  CA  26  44  16  77  55
2017/05/04  FI  3   11  37  38  18
2017/05/04  GE  70  14  75  86  55
2017/05/04  MI  71  53  39  6   30
2017/05/04  NA  73  20  12  13  54
2017/05/04  PA  37  49  33  80  61
2017/05/04  RM  39  60  75  32  2
2017/05/04  RN  40  78  43  75  55
2017/05/04  TO  24  87  52  90  16
2017/05/04  VE  14  54  25  61  63
2017/05/06  BA  15  31  20  78  66
2017/05/06  CA  84  33  47  9   45
2017/05/06  FI  17  48  33  49  20
2017/05/06  GE  16  51  62  21  72
2017/05/06  MI  12  83  64  62  2
2017/05/06  NA  15  80  52  5   89
2017/05/06  PA  2   33  65  72  61
2017/05/06  RM  29  49  13  70  83
2017/05/06  RN  30  81  55  37  41

As you can see this is formatted this way:

DATE(tab)LETTERS(tab)N1(tab)N2(tab)N3(tab)N4(tab)N5(tab)

The second line has the same date, and changes LETTERS and N_.

I need to insert this file into a DB but this has to have one line for every date, like so:

 Date       |BA1|BA2|BA3|BA4|BA5|CA1|CA2|CA3|CA4|CA5|FI1|FI2|FI3|FI4|FI5|
-----------------------------------------------------------------  
2017/05/02| 86 | 23| 39| 89|5  |23 | 4 | 14|86 |5.....
-----------------------------------------------------------------  
2017/05/04|61  |70 |47 |71 |75 |26 |44 |16 |77 |55...

Why my code is not working?

public function caricaStorico(){
        $this->load->helper('file');

        $storico = read_file(base_url("storico.txt"));

        $splitter = explode("\n", $storico);
        $data_estr = "-";
        $insert = [];
        $arr_key = 0;

        echo "<pre>";

        foreach ($splitter as $key => $value) {
            $dati = explode("\t", $value);

            if(count($dati)>1) {

                $data_estr = $dati[0];

                $ruota = $dati[1];
                $n1 = $dati[2];
                $n2 = $dati[3];
                $n3 = $dati[4];
                $n4 = $dati[5];
                $n5 = $dati[6];

                switch ($ruota) {
                    case 'BA':
                        array_push($insert, ["data_estrazione" => $data_estr, "BA1" => $n1, 'BA2' => $n2, "BA3" => $n3, "BA4" => $n4, "BA5" => $n5]);
                        break;

                    case 'FI':
                        $insert[$arr_key] += ["FI1" => $n1, 'FI2' => $n2, "FI3" => $n3, "FI4" => $n4, "FI5" => $n5];
                        break;

                    case 'MI':
                        $insert[$arr_key] += ["MI1" => $n1, 'MI2' => $n2, "MI3" => $n3, "MI4" => $n4, "MI5" => $n5];
                        break;

                    case 'NA':
                        $insert[$arr_key] += ["NA1" => $n1, 'NA2' => $n2, "NA3" => $n3, "NA4" => $n4, "NA5" => $n5];
                        break;

                    case 'PA':
                        $insert[$arr_key] += ["PA1" => $n1, 'PA2' => $n2, "PA3" => $n3, "PA4" => $n4, "PA5" => $n5];
                        break;

                    case 'RM':
                        $insert[$arr_key] += ["RM1" => $n1, 'RM2' => $n2, "RM3" => $n3, "RM4" => $n4, "RM5" => $n5];
                        break;

                    case 'TO':
                        $insert[$arr_key] += ["TO1" => $n1, 'TO2' => $n2, "TO3" => $n3, "TO4" => $n4, "TO5" => $n5];
                        break;

                    case 'VE':
                        $insert[$arr_key] += ["VE1" => $n1, 'VE2' => $n2, "VE3" => $n3, "VE4" => $n4, "VE5" => $n5];
                        break;

                    case 'RN':
                        $insert[$arr_key] += ["NZ1" => $n1, 'NZ2' => $n2, "NZ3" => $n3, "NZ4" => $n4, "NZ5" => $n5];
                        break;
                }
            }
            $arr_key = $arr_key+1;  
        }

        print_r($insert);
        echo "</pre>";

    }

If I remove the $input[$arr_key] and I do $input[0] I get the right result only on the first array while the others are only containing the BA numbers, as you can see:

Array
(
    [0] => Array
        (
            [data_estrazione] => 1939/01/07
            [BA1] => 58
            [BA2] => 22
            [BA3] => 47
            [BA4] => 49
            [BA5] => 69
            [FI1] => 27
            [FI2] => 57
            [FI3] => 81
            [FI4] => 43
            [FI5] => 61
            [MI1] => 40
            [MI2] => 38
            [MI3] => 57
            [MI4] => 67
            [MI5] => 7
            [NA1] => 85
            [NA2] => 44
            [NA3] => 48
            [NA4] => 88
            [NA5] => 55
            [PA1] => 73
            [PA2] => 80
            [PA3] => 39
            [PA4] => 38
            [PA5] => 57
            [RM1] => 73
            [RM2] => 24
            [RM3] => 4
            [RM4] => 39
            [RM5] => 22
            [TO1] => 19
            [TO2] => 43
            [TO3] => 10
            [TO4] => 31
            [TO5] => 27
            [VE1] => 9
            [VE2] => 43
            [VE3] => 61
            [VE4] => 14
            [VE5] => 75
            [NZ1] => 72
            [NZ2] => 12
            [NZ3] => 42
            [NZ4] => 57
            [NZ5] => 31
        )

    [1] => Array
        (
            [data_estrazione] => 1939/01/14
            [BA1] => 18
            [BA2] => 77
            [BA3] => 33
            [BA4] => 62
            [BA5] => 19
        )

    [2] => Array
        (
            [data_estrazione] => 1939/01/21
            [BA1] => 68
            [BA2] => 65
            [BA3] => 41
            [BA4] => 28
            [BA5] => 67
        )

    [3] => Array
        (
            [data_estrazione] => 1939/01/28
            [BA1] => 76
            [BA2] => 55
            [BA3] => 48
            [BA4] => 85
            [BA5] => 71
        )

    [4] => Array
        (
            [data_estrazione] => 1939/02/04
            [BA1] => 70
            [BA2] => 2
            [BA3] => 20
            [BA4] => 85
            [BA5] => 75
        )

    [5] => Array
        (
            [data_estrazione] => 1939/02/11
            [BA1] => 82
            [BA2] => 81
            [BA3] => 16
            [BA4] => 52
            [BA5] => 77
        )

1 Answer 1

1

You can achive this via using date as a key of array check this out: I used string variable to demonstrate your case so you can change storico variable as you wish.

<?php
 function caricaStorico(){
        $storico = '2017/05/02  BA  86  23  39  89  5, 2017/05/02  CA  23  4   14  86  5, 2017/05/03  BA  86  23  39  89  5, 2017/05/03  CA  23  4   14  86  5';
        $splitter = explode(",", $storico);
        $data_estr = "-";
        $insert = [];

        echo "<pre>";
        foreach ($splitter as $key => $value) {
            $dati = explode("  ", $value);

            if(count($dati)>1) {

                $data_estr = trim($dati[0]);

                $ruota = $dati[1];
                $n1 = $dati[2];
                $n2 = $dati[3];
                $n3 = $dati[4];
                $n4 = $dati[5];
                $n5 = $dati[6];

                if(isset($insert[$data_estr]) && is_array($insert[$data_estr]) )
                   $insert[$data_estr] += [ "data_estrazione" => $data_estr,  $ruota."1" => $n1, $ruota."2" => $n2, $ruota."3" => $n3, $ruota."4" => $n4, $ruota."5" => $n5 ];
                else 
                    $insert[$data_estr] = array("data_estrazione" => $data_estr, $ruota."1" => $n1, $ruota."2" => $n2, $ruota."3" => $n3, $ruota."4" => $n4, $ruota."5" => $n5 );
             }
        }

        print_r($insert);
        echo "</pre>";

    }

     caricaStorico();
?>

output to this code:

 Array
(
    [2017/05/02] => Array
        (
            [data_estrazione] => 2017/05/02
            [BA1] => 86
            [BA2] => 23
            [BA3] => 39
            [BA4] => 89
            [BA5] => 5
            [CA1] => 23
            [CA2] => 4
            [CA3] =>  14
            [CA4] => 86
            [CA5] => 5
        )

    [2017/05/03] => Array
        (
            [data_estrazione] => 2017/05/03
            [BA1] => 86
            [BA2] => 23
            [BA3] => 39
            [BA4] => 89
            [BA5] => 5
            [CA1] => 23
            [CA2] => 4
            [CA3] =>  14
            [CA4] => 86
            [CA5] => 5
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

That is it! :) Tks

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.