How can I group and add this type of array?

0

I have a question for hours trying to solve it but I have not found any solution.

I have an arrangement that comes from a bd which is dynamic between months and quantities, which is to use it to show some graphs, what I need is to group the months and add their respective values. Any solution? Thanks in advance.

 {"data":[["Mes","CLP"],["Nov",290709],["Dic",9283],["Ene",234066],["Ene",114465]]}

The idea is to keep the months grouped together and the amounts added, in this case like this:

{"data":[["Mes","CLP"],["Nov",290709],["Dic",9283],["Ene",348531]]}

The query is the following

public function graficoMesesTecnico($tecnico,$desde,$hasta,$tipo){
        $this->db->select("SHA1(i.id) as 'hash_id',
            i.id as id,
            it.tipo as especialidad,
            it.id as id_especialidad,
            it.precio_baremo_1 as precio_baremo_1,
            it.precio_baremo_2 as precio_baremo_2,
            it.porcentaje_maestro as porcentaje_maestro,
            it.porcentaje_ayudante1 as porcentaje_ayudante1,
            it.porcentaje_ayudante2 as porcentaje_ayudante2,

            i.maestro as rut_maestro,
            i.maestro2 as rut_maestro2,
            i.ayudante1 as rut_ayudante1,
            i.ayudante2 as rut_ayudante2,
            CONCAT(u1.primer_nombre,' ',u1.apellido_paterno) as 'maestro',
            CONCAT(u2.primer_nombre,' ',u2.apellido_paterno) as 'ayudante1',
            CONCAT(u3.primer_nombre,' ',u3.apellido_paterno) as 'ayudante2',
            CONCAT(u5.primer_nombre,' ',u5.apellido_paterno) as 'maestro2'
            ");

            $this->db->from('movistar_informes_de_avance as i');
            $this->db->join('usuario as u1', 'u1.rut = i.maestro', 'left');
            $this->db->join('usuario as u2', 'u2.rut = i.ayudante1', 'left');
            $this->db->join('usuario as u3', 'u3.rut = i.ayudante2', 'left');
            $this->db->join('usuario as u5', 'u5.rut = i.maestro2', 'left');
            $this->db->join('movistar_especialidades as it', 'it.id = i.id_tipo', 'left');
            $this->db->where('i.estado', 3);

            if($tipo=="1"){
                $this->db->where('(i.ayudante1="'.$tecnico.'" or i.ayudante2="'.$tecnico.'")');
            }else if($tipo=="3"){
                $this->db->where('i.maestro', $tecnico);
            }

            $this->db->where("i.fecha_ingreso BETWEEN '".$desde."' AND '".$hasta."'");              
            $this->db->order_by('i.fecha_ingreso', 'asc');
            $res=$this->db->get();
            //echo $this->db->last_query();

            $temp=array();
            $temp[] = array("Mes","CLP"); 
            $puntos_baremos=0;

        foreach($res->result_array() as $key){
            $this->db->select('(CAST(mmo.puntos_baremos AS DECIMAL(10,3))*sum(md.cantidad_mano_obra)) as "totalpb",
                MONTH(m.fecha_ingreso) as mes',false);

            $this->db->from('movistar_informes_de_avance_detalle as md');
            $this->db->join('movistar_informes_de_avance as m', 'm.id = md.id_informe', 'left');
            $this->db->join('movistar_mano_de_obra as mmo', 'mmo.id = md.id_mano_obra', 'left');
            $this->db->join('movistar_unidad_de_obra as muo', 'muo.id = md.id_unidad_obra', 'left');
            $this->db->where('id_informe', $key["id"]);

            $this->db->group_by('md.id_mano_obra');
            /*$this->db->group_by('MONTH(m.fecha_ingreso)');
            $this->db->group_by('YEAR(m.fecha_ingreso)');*/

            $res2=$this->db->get();
            foreach($res2->result_array() as $key2){
                $puntos_baremos=$puntos_baremos+(floatval($key2["totalpb"]));
            }


            if($tipo==1){//AYUDANTE

                $nombre=$key["ayudante1"];
                $str="0.".$key["porcentaje_ayudante1"];
                $baremos_individual=$puntos_baremos*(float)$str;
                $clp_individual=$baremos_individual*$key["precio_baremo_1"];

            }elseif($tipo==3) {//MAESTRO

                $nombre=$key["maestro"];
                $str="0.".$key["porcentaje_maestro"];
                $baremos_individual=$puntos_baremos*(float)$str;
                $clp_individual=$baremos_individual*$key["precio_baremo_1"];
            }

            $temp[] = array($this->mes_corto($key2["mes"]),(float)round($clp_individual)); 
            $puntos_baremos=0;
        }

        $filas = $temp;
        return $filas;
    }
    
asked by Ricardo Antonio Hernández 10.01.2018 в 16:29
source

1 answer

0

I hope this helps you:

var data = {"data":[["Mes","CLP"],["Nov",290709],["Dic",9283],["Ene",234066],["Ene",114465]]};

var months = {};

data.data.forEach(function( dat ) {

  var m = dat[0], //mes
  	  n = parseInt( dat[1] ); //valor
      
  /* Esto es simplemente para tomar solo los valores numéricos */
  if ( !n ) return;
	
  if ( !months.hasOwnProperty( m ) ) {
  	months[m] = n;
  } else {
  	months[m] += n;
  }

});

console.log( months );

Keep in mind that in this way you get an object with the months in order as long as they are received in order of the database, if you do not have to make some more adjustments.

With the resulting object you can work comfortably with the values, this can be done in many ways, but in this way that I have shown you can be quite useful. This is an example in JavaScript but it would not be very different in PHP, it is mostly to give you an idea.

    
answered by 10.01.2018 / 16:47
source