Pass an array to another format

Asked

Viewed 39 times

-3

The array is coming in this database format:

array (size=12)
  0 => 
    array (size=3)
      'maquinas' => string '111' (length=3)
      'mes' => string '1' (length=1)
      'YEAR(data_ordem)' => string '2016' (length=4)
  1 => 
    array (size=3)
      'maquinas' => string '99' (length=2)
      'mes' => string '2' (length=1)
      'YEAR(data_ordem)' => string '2016' (length=4)
  2 => 
    array (size=3)
      'maquinas' => string '116' (length=3)
      'mes' => string '3' (length=1)
      'YEAR(data_ordem)' => string '2016' (length=4)

I need him to stay that way:

$dados = array(
    array('Janeiro', 10),
    array('Fevereiro', 10),
    array('Março', 10),
    array('Abril', 30),

But I’m not getting it.

  • And where are these whole months associated?

  • The month that is in number you want to turn in full and in English, this is the problem? those 10 and the 30 I have no idea what it is.

  • 1

    January, February, March and April we can deduce that is the number of the month (it remains to be seen whether it starts at 0 or 1). Now the 10,10,10,30, as Marcelo said, only the top array can not deduce what they are. They would be the number of machines?

2 answers

-1

You can do it this way:

<?php 

function mesExtenso($mes){
    return [
        1 => 'Janeiro',
        'Fevereiro',
        'Março',
        'Abril',
        'Maio',
        'Junho',
        'Julho',
        'Agosto',
        'Setembro',
        'Outubro',
        'Novembro',
        'Dezembro'
    ][$mes];
}

$data = [
    [
        'maquinas' =>  '111',
        'mes' =>  '1',
        'YEAR(data_ordem)' =>  '2016'
    ],
    [
        'maquinas' =>  '99',
        'mes' =>  '2',
        'YEAR(data_ordem)' =>  '2016'
    ],
    [
        'maquinas' =>  '116',
        'mes' =>  '3',
        'YEAR(data_ordem)' =>  '2016'
    ]
];

$return = [];

foreach ($data as $key => $value) {
    $return[] = [
        mesExtenso($value['mes']),
        $value['maquinas']
    ];
}

var_dump($return);

Will return:

array (size=3)
  0 => 
    array (size=2)
      0 => string 'Janeiro' (length=7)
      1 => string '111' (length=3)
  1 => 
    array (size=2)
      0 => string 'Fevereiro' (length=9)
      1 => string '99' (length=2)
  2 => 
    array (size=2)
      0 => string 'Março' (length=6)
      1 => string '116' (length=3)

Note: Since you didn’t mention which numbers are associated with the month, I used as an example the key maquinas.

Testing on the ideone

  • 1

    Could they at least state why downvote so that the problem can be resolved in the answer, if it has.

-2


Try this:

$array_meses = array();
$array_meses[1] = "Janeiro";
$array_meses[2] = "Fevereiro";
$array_meses[3] = "Março";
$array_meses[4] = "Abril";
$array_meses[5] = "Maio";
$array_meses[6] = "Junho";
$array_meses[7] = "Julho";
$array_meses[8] = "Agosto";
$array_meses[9] = "Setembro";
$array_meses[10] = "Outubro";
$array_meses[11] = "Novembro";
$array_meses[12] = "Dezembro";

$novo_array = array();

foreach($array_banco as $value)
{
     $novo_array[] = array($array_meses[(int) $value["mes"]], $value["maquinas"]);
}

print_r($novo_array); 
  • Thanks man, that’s just what I needed! Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.