我正在使用谷歌排行榜。我对第一个结果有疑问。
我需要删除“行”中的第一个结果。 我尝试使用array_shift,但我有一个mystake。 你能帮我吗?
$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);
结果:
{
"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"
}
]
}
]
}
答案 0 :(得分:0)
如果您不想要的行的索引为零,则可以尝试此操作。它在表['rows']填充之前有一个条件
$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);
或者您可以在编码数据之前删除$ table ['rows']中的第一个
$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);