Home > Software design >  for loop in an array inside another array, get every value separatedly
for loop in an array inside another array, get every value separatedly

Time:11-19

How are you? Fine I hope!

I have a problem with arrays, already searched here.

This is the array:

array (
  0 => 
  array (
    'column_title' => 'cod',
    'values' => 
    array (
      0 => 
      array (
        'valor' => '0301',
      ),
      1 => 
      array (
        'valor' => '3545',
      ),
      2 => 
      array (
        'valor' => 3552,
      ),
      3 => 
      array (
        'valor' => '3547',
      ),
      4 => 
      array (
        'valor' => 3553,
      ),
      5 => 
      array (
        'valor' => '0303',
      ),
      6 => 
      array (
        'valor' => 3554,
      ),
      7 => 
      array (
        'valor' => '3549',
      ),
      8 => 
      array (
        'valor' => 3555,
      ),
      9 => 
      array (
        'valor' => 'M025',
      ),
      10 => 
      array (
        'valor' => 'M030',
      ),
      11 => 
      array (
        'valor' => 'M035',
      ),
      12 => 
      array (
        'valor' => 'M040',
      ),
      13 => 
      array (
        'valor' => 'M045',
      ),
      14 => 
      array (
        'valor' => 'M050',
      ),
      15 => 
      array (
        'valor' => 'V055',
      ),
      16 => 
      array (
        'valor' => 'V060',
      ),
      17 => 
      array (
        'valor' => 'V065',
      ),
      18 => 
      array (
        'valor' => 'V070',
      ),
      19 => 
      array (
        'valor' => 'V075',
      ),
      20 => 
      array (
        'valor' => 'V080',
      ),
      21 => 
      array (
        'valor' => 'V085',
      ),
      22 => 
      array (
        'valor' => 'V090',
      ),
      23 => 
      array (
        'valor' => 'V095',
      ),
      24 => 
      array (
        'valor' => 'V100',
      ),
      25 => 
      array (
        'valor' => 'M025R',
      ),
      26 => 
      array (
        'valor' => 'M030R',
      ),
      27 => 
      array (
        'valor' => 'M035R',
      ),
      28 => 
      array (
        'valor' => 'M040R',
      ),
      29 => 
      array (
        'valor' => 'M045R',
      ),
      30 => 
      array (
        'valor' => 'M050R',
      ),
      31 => 
      array (
        'valor' => 'V055R',
      ),
      32 => 
      array (
        'valor' => 'V060R',
      ),
      33 => 
      array (
        'valor' => 'V065R',
      ),
      34 => 
      array (
        'valor' => 'V070R',
      ),
      35 => 
      array (
        'valor' => 'V075R',
      ),
      36 => 
      array (
        'valor' => 'V080R',
      ),
      37 => 
      array (
        'valor' => 'V085R',
      ),
      38 => 
      array (
        'valor' => 'V090R',
      ),
      39 => 
      array (
        'valor' => 'V095R',
      ),
      40 => 
      array (
        'valor' => 'V100R',
      ),
    ),
  ),
  1 => 
  array (
    'column_title' => 'tipo1',
    'values' => 
    array (
      0 => 
      array (
        'valor' => 'Integral',
      ),
      1 => 
      array (
        'valor' => 'Integral',
      ),
      2 => 
      array (
        'valor' => 'Integral',
      ),
      3 => 
      array (
        'valor' => 'Integral',
      ),
      4 => 
      array (
        'valor' => 'Integral',
      ),
      5 => 
      array (
        'valor' => 'Integral',
      ),
      6 => 
      array (
        'valor' => 'Integral',
      ),
      7 => 
      array (
        'valor' => 'Integral',
      ),
      8 => 
      array (
        'valor' => 'Integral',
      ),
      9 => 
      array (
        'valor' => 'Integral',
      ),
      10 => 
      array (
        'valor' => 'Integral',
      ),
      11 => 
      array (
        'valor' => 'Integral',
      ),
      12 => 
      array (
        'valor' => 'Integral',
      ),
      13 => 
      array (
        'valor' => 'Integral',
      ),
      14 => 
      array (
        'valor' => 'Integral',
      ),
      15 => 
      array (
        'valor' => 'Integral',
      ),
      16 => 
      array (
        'valor' => 'Integral',
      ),
      17 => 
      array (
        'valor' => 'Integral',
      ),
      18 => 
      array (
        'valor' => 'Integral',
      ),
      19 => 
      array (
        'valor' => 'Integral',
      ),
      20 => 
      array (
        'valor' => 'Integral',
      ),
      21 => 
      array (
        'valor' => 'Integral',
      ),
      22 => 
      array (
        'valor' => 'Integral',
      ),
      23 => 
      array (
        'valor' => 'Integral',
      ),
      24 => 
      array (
        'valor' => 'Integral',
      ),
      25 => 
      array (
        'valor' => 'Reduzido',
      ),
      26 => 
      array (
        'valor' => 'Reduzido',
      ),
      27 => 
      array (
        'valor' => 'Reduzido',
      ),
      28 => 
      array (
        'valor' => 'Reduzido',
      ),
      29 => 
      array (
        'valor' => 'Reduzido',
      ),
      30 => 
      array (
        'valor' => 'Reduzido',
      ),
      31 => 
      array (
        'valor' => 'Reduzido',
      ),
      32 => 
      array (
        'valor' => 'Reduzido',
      ),
      33 => 
      array (
        'valor' => 'Reduzido',
      ),
      34 => 
      array (
        'valor' => 'Reduzido',
      ),
      35 => 
      array (
        'valor' => 'Reduzido',
      ),
      36 => 
      array (
        'valor' => 'Reduzido',
      ),
      37 => 
      array (
        'valor' => 'Reduzido',
      ),
      38 => 
      array (
        'valor' => 'Reduzido',
      ),
      39 => 
      array (
        'valor' => 'Reduzido',
      ),
      40 => 
      array (
        'valor' => 'Reduzido',
      ),
    ),
  ),
  6 => 
  array (
    'column_title' => 'm72',
    'values' => 
    array (
      0 => 
      array (
        'valor' => 188.75394,
      ),
      1 => 
      array (
        'valor' => 232.446981666667,
      ),
      2 => 
      array (
        'valor' => 237.690146666667,
      ),
      3 => 
      array (
        'valor' => 260.410528333333,
      ),
      4 => 
      array (
        'valor' => 270.896858333333,
      ),
      5 => 
      array (
        'valor' => 293.61724,
      ),
      6 => 
      array (
        'valor' => 300.608126666667,
      ),
      7 => 
      array (
        'valor' => 321.580786666667,
      ),
      8 => 
      array (
        'valor' => 343.019564035167,
      ),
      9 => 
      array (
        'valor' => 0,
      ),
      10 => 
      array (
        'valor' => 0,
      ),
      11 => 
      array (
        'valor' => 0,
      ),
      12 => 
      array (
        'valor' => 0,
      ),
      13 => 
      array (
        'valor' => 0,
      ),
      14 => 
      array (
        'valor' => 0,
      ),
      15 => 
      array (
        'valor' => 0,
      ),
      16 => 
      array (
        'valor' => 0,
      ),
      17 => 
      array (
        'valor' => 0,
      ),
      18 => 
      array (
        'valor' => 0,
      ),
      19 => 
      array (
        'valor' => 0,
      ),
      20 => 
      array (
        'valor' => 0,
      ),
      21 => 
      array (
        'valor' => 0,
      ),
      22 => 
      array (
        'valor' => 0,
      ),
      23 => 
      array (
        'valor' => 0,
      ),
      24 => 
      array (
        'valor' => 0,
      ),
      25 => 
      array (
        'valor' => 0,
      ),
      26 => 
      array (
        'valor' => 0,
      ),
      27 => 
      array (
        'valor' => 0,
      ),
      28 => 
      array (
        'valor' => 0,
      ),
      29 => 
      array (
        'valor' => 0,
      ),
      30 => 
      array (
        'valor' => 0,
      ),
      31 => 
      array (
        'valor' => 0,
      ),
      32 => 
      array (
        'valor' => 0,
      ),
      33 => 
      array (
        'valor' => 0,
      ),
      34 => 
      array (
        'valor' => 0,
      ),
      35 => 
      array (
        'valor' => 0,
      ),
      36 => 
      array (
        'valor' => 0,
      ),
      37 => 
      array (
        'valor' => 0,
      ),
      38 => 
      array (
        'valor' => 0,
      ),
      39 => 
      array (
        'valor' => 0,
      ),
      40 => 
      array (
        'valor' => 0,
      ),
    ),
  ),
  7 => 
  array (
    'column_title' => 'm70',
    'values' => 
    array (
      0 => 
      array (
        'valor' => 193.936911428571,
      ),
      1 => 
      array (
        'valor' => 238.829715,
      ),
      2 => 
      array (
        'valor' => 244.216851428571,
      ),
      3 => 
      array (
        'valor' => 267.561109285714,
      ),
      4 => 
      array (
        'valor' => 278.335382142857,
      ),
      5 => 
      array (
        'valor' => 301.67964,
      ),
      6 => 
      array (
        'valor' => 308.862488571429,
      ),
      7 => 
      array (
        'valor' => 330.411034285714,
      ),
      8 => 
      array (
        'valor' => 352.4384964285,
      ),
      9 => 
      array (
        'valor' => 0,
      ),
      10 => 
      array (
        'valor' => 0,
      ),
      11 => 
      array (
        'valor' => 0,
      ),
      12 => 
      array (
        'valor' => 0,
      ),
      13 => 
      array (
        'valor' => 0,
      ),
      14 => 
      array (
        'valor' => 0,
      ),
      15 => 
      array (
        'valor' => 0,
      ),
      16 => 
      array (
        'valor' => 0,
      ),
      17 => 
      array (
        'valor' => 0,
      ),
      18 => 
      array (
        'valor' => 0,
      ),
      19 => 
      array (
        'valor' => 0,
      ),
      20 => 
      array (
        'valor' => 0,
      ),
      21 => 
      array (
        'valor' => 0,
      ),
      22 => 
      array (
        'valor' => 0,
      ),
      23 => 
      array (
        'valor' => 0,
      ),
      24 => 
      array (
        'valor' => 0,
      ),
      25 => 
      array (
        'valor' => 0,
      ),
      26 => 
      array (
        'valor' => 0,
      ),
      27 => 
      array (
        'valor' => 0,
      ),
      28 => 
      array (
        'valor' => 0,
      ),
      29 => 
      array (
        'valor' => 0,
      ),
      30 => 
      array (
        'valor' => 0,
      ),
      31 => 
      array (
        'valor' => 0,
      ),
      32 => 
      array (
        'valor' => 0,
      ),
      33 => 
      array (
        'valor' => 0,
      ),
      34 => 
      array (
        'valor' => 0,
      ),
      35 => 
      array (
        'valor' => 0,
      ),
      36 => 
      array (
        'valor' => 0,
      ),
      37 => 
      array (
        'valor' => 0,
      ),
      38 => 
      array (
        'valor' => 0,
      ),
      39 => 
      array (
        'valor' => 0,
      ),
      40 => 
      array (
        'valor' => 0,
      ),
    ),
  ),
)

      

And I tried this code:

$cod = '';
$data = '';
$prazo = '';
$titulos=array('cod','tipo1','tipo2');
for ($i = 0; $i < count($parcelas); $i  ) {
    for ($col = 0; $col < count($parcelas[$i]['values']); $col  ) {
        foreach ($parcelas[0]['values'][$col] as $item){
            $cod = $item;
            echo $cod . '<br>';  
            if (!in_array($parcelas[$i]['column_title'], $titulos)) {
                for ($col = 0; $col < count($parcelas[$i]['values']); $col  ) {
                    foreach ($parcelas[$i]['values'][$col] as $item){
                        if ($item != 0) {
                            $prazo = substr($parcelas[$i]['column_title'],1);
                            echo 'codigo '. $cod . ' dados = '. $item . ' prazo ' . $prazo . '<br>';
                        }
                    }
                }
            }                 
        }
    }

What I need: Store every data of the arrays separatedly, but inserting togheter the number of months (prazo) and the cod that is in outside the second array, with my code I can separete data, but the code is always the first one...

cod is in the first array, is unique value

the other array titulo is some columns that I want to ignore

I need to get every value in the columns excluding the column code, and every value must be related with the cod value of his row

Any hint please?

CodePudding user response:

If column_title is unique then you can index on that and extract the values. Then you can extract the valor values from there:

foreach(array_column($parcelas, 'values', 'column_title') as $condigo => $values) {
    foreach(array_column($values, 'valor') as $value) {
        echo "$condigo $value";
    }
}

It's hard to follow what else you are doing with vars, output, in_array etc. but just use this approach and adapt.

CodePudding user response:

Maybe that what you are looking for:

function getMaxValues(array $array): int
{
    $max = 0;
    foreach ($array as $data) {
        if (count($data['values']) > $max) {
            $max = count($data['values']);
        }
    }
    return $max;
}

function mergeColumns(array $array): array
{
    $max = getMaxValues($array);
    $result = [];
    for ($i = 0; $i < $max; $i  ) {
        $subResult = [];
        foreach ($array as $data) {
            if (isset($data['values'][$i])) {
                $subResult[$data['column_title']] = $data['values'][$i];
            } else {
                $subResult[$data['column_title']] = null;
            }
        }
        $result[] = $subResult;
    }
    return $result;
}

$array = [
    [
        "column_title" => "cod",
        "values" => [
            [
                "valor" => "0301"
            ],
            [
                "valor" => "3545"
            ],
            [
                "valor" => 3552
            ],
            [
                "valor" => "3547"
            ],
            [
                "valor" => 3553
            ],
            [
                "valor" => "0303"
            ],
            [
                "valor" => 3554
            ],
            [
                "valor" => "3549"
            ],
            [
                "valor" => 3555
            ],
            [
                "valor" => "M025"
            ],
            [
                "valor" => "M030"
            ],
            [
                "valor" => "M035"
            ],
            [
                "valor" => "M040"
            ],
            [
                "valor" => "M045"
            ],
            [
                "valor" => "M050"
            ],
            [
                "valor" => "V055"
            ],
            [
                "valor" => "V060"
            ],
            [
                "valor" => "V065"
            ],
            [
                "valor" => "V070"
            ],
            [
                "valor" => "V075"
            ],
            [
                "valor" => "V080"
            ],
            [
                "valor" => "V085"
            ],
            [
                "valor" => "V090"
            ],
            [
                "valor" => "V095"
            ],
            [
                "valor" => "V100"
            ],
            [
                "valor" => "M025R"
            ],
            [
                "valor" => "M030R"
            ],
            [
                "valor" => "M035R"
            ],
            [
                "valor" => "M040R"
            ],
            [
                "valor" => "M045R"
            ],
            [
                "valor" => "M050R"
            ],
            [
                "valor" => "V055R"
            ],
            [
                "valor" => "V060R"
            ],
            [
                "valor" => "V065R"
            ],
            [
                "valor" => "V070R"
            ],
            [
                "valor" => "V075R"
            ],
            [
                "valor" => "V080R"
            ],
            [
                "valor" => "V085R"
            ],
            [
                "valor" => "V090R"
            ],
            [
                "valor" => "V095R"
            ],
            [
                "valor" => "V100R"
            ]
        ]
    ],
    [
        "column_title" => "tipo1",
        "values" => [
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Integral"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ],
            [
                "valor" => "Reduzido"
            ]
        ]
    ],
    [
        "column_title" => "m72",
        "values" => [
            [
                "valor" => 188.75394
            ],
            [
                "valor" => 232.44698166667
            ],
            [
                "valor" => 237.69014666667
            ],
            [
                "valor" => 260.41052833333
            ],
            [
                "valor" => 270.89685833333
            ],
            [
                "valor" => 293.61724
            ],
            [
                "valor" => 300.60812666667
            ],
            [
                "valor" => 321.58078666667
            ],
            [
                "valor" => 343.01956403517
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ]
        ]
    ],
    [
        "column_title" => "m70",
        "values" => [
            [
                "valor" => 193.93691142857
            ],
            [
                "valor" => 238.829715
            ],
            [
                "valor" => 244.21685142857
            ],
            [
                "valor" => 267.56110928571
            ],
            [
                "valor" => 278.33538214286
            ],
            [
                "valor" => 301.67964
            ],
            [
                "valor" => 308.86248857143
            ],
            [
                "valor" => 330.41103428571
            ],
            [
                "valor" => 352.4384964285
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ],
            [
                "valor" => 0
            ]
        ]
    ]
];

$array = mergeColumns($array);

var_dump($array);

Result of this code can be found here: http://sandbox.onlinephpfunctions.com/code/7099ad5d97b089bc323923b11e827fe9419c859e

  • Related