Skip to content
Advertisement

Remove first result in JSON

I’m using GoogleCharts. I have a problem with the first result. I need to remove the first result in “rows”. I tried with array_shift but I got an error.

$q = Database::connect()->prepare("SELECT p.nom_cat,
SUM(CASE WHEN YEAR(date_devis) = 2014 THEN d.prix_ht_prod ELSE 0 END) '2014', 
SUM(CASE WHEN YEAR(date_devis) = 2015 THEN d.prix_ht_prod ELSE 0 END) '2015', 
FROM devis d INNER JOIN produit p USING(id_produit) GROUP BY p.nom_cat ORDER BY YEAR(date_devis)");
$q -> execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
$table['cols'][] = array('label' => 'produit', 'type' => 'string');
foreach($q as $res) {
   $table['cols'][] = array('label' => $res['nom_cat'], 'type' => 'number');
   if(empty($listeAnnees)) {
      foreach(($listeAnnees = array_keys($res)) as $i => $annees) {
        $table['rows'][$i]['c'][] = array('v' => $annees);
      }
   }
   foreach($res as $k => $v) {
      $i = array_search($k, $listeAnnees);
      $table['rows'][$i]['c'][] = array('v' => $v);
   }
}
$jsonData = json_encode($table);
//$jsonData = json_encode(array_splice(json_decode($jsonData, true), 1));
print_r($jsonData);

The result:

    {  
   "cols":[  
      {  
         "label":"produit",
         "type":"string"
      },
      {  
         "label":"Cat1",
         "type":"number"
      },
      {  
         "label":"Cat2",
         "type":"number"
      },
      {  
         "label":"Cat3",
         "type":"number"
      }
   ],
   "rows":[  
      {  
         "c":[  //i need to remove this one
            {  
               "v":"nom_cat"
            },
            {  
               "v":"Cat1"
            },
            {  
               "v":"Cat2"
            },
            {  
               "v":"Cat3"
            }
         ]
      },
      {  
         "c":[  
            {  
               "v":2014
            },
            {  
               "v":"15000.00"
            },
            {  
               "v":"50000.00"
            },
            {  
               "v":"0.00"
            }
         ]
      },
      {  
         "c":[  
            {  
               "v":2015
            },
            {  
               "v":"20000.00"
            },
            {  
               "v":"1000.00"
            },
            {  
               "v":"24100.50"
            }
         ]
      }
   ]
}

Advertisement

Answer

If the index of the row that you don’t want is zero you could try this. It has a condition before the table[‘rows’] is filled

$q = Database::connect()->prepare("SELECT p.nom_cat,
SUM(CASE WHEN YEAR(date_devis) = 2014 THEN d.prix_ht_prod ELSE 0 END) '2014', 
SUM(CASE WHEN YEAR(date_devis) = 2015 THEN d.prix_ht_prod ELSE 0 END) '2015', 
FROM devis d INNER JOIN produit p USING(id_produit) GROUP BY p.nom_cat ORDER BY YEAR(date_devis)");
$q -> execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
$table['cols'][] = array('label' => 'produit', 'type' => 'string');
foreach($q as $res) {
   $table['cols'][] = array('label' => $res['nom_cat'], 'type' => 'number');
   if(empty($listeAnnees)) {
      foreach(($listeAnnees = array_keys($res)) as $i => $annees) {
        if($i!==0){
          $table['rows'][$i]['c'][] = array('v' => $annees);
        }
      }
   }
   foreach($res as $k => $v) {
      $i = array_search($k, $listeAnnees);
      if($i!==0){
        $table['rows'][$i]['c'][] = array('v' => $v);
      }
   }
}
$jsonData = json_encode($table);
//$jsonData = json_encode(array_splice(json_decode($jsonData, true), 1));
print_r($jsonData);

Or you could remove the first in $table[‘rows’] just before encoding the data

$q = Database::connect()->prepare("SELECT p.nom_cat,
SUM(CASE WHEN YEAR(date_devis) = 2014 THEN d.prix_ht_prod ELSE 0 END) '2014', 
SUM(CASE WHEN YEAR(date_devis) = 2015 THEN d.prix_ht_prod ELSE 0 END) '2015', 
FROM devis d INNER JOIN produit p USING(id_produit) GROUP BY p.nom_cat ORDER BY YEAR(date_devis)");
$q -> execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
$table['cols'][] = array('label' => 'produit', 'type' => 'string');
foreach($q as $res) {
   $table['cols'][] = array('label' => $res['nom_cat'], 'type' => 'number');
   if(empty($listeAnnees)) {
      foreach(($listeAnnees = array_keys($res)) as $i => $annees) {
        $table['rows'][$i]['c'][] = array('v' => $annees);
      }
   }
   foreach($res as $k => $v) {
      $i = array_search($k, $listeAnnees);
      $table['rows'][$i]['c'][] = array('v' => $v);
   }
}

$new_rows = $table["rows"];
array_shift($new_rows);
$table["rows"] = $new_rows;

$jsonData = json_encode($table);
//$jsonData = json_encode(array_splice(json_decode($jsonData, true), 1));
print_r($jsonData);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement