Skip to content

Instantly share code, notes, and snippets.

@gladson
Last active September 4, 2020 22:05
Show Gist options
  • Save gladson/98114b2225defe617fb67d12aed1803d to your computer and use it in GitHub Desktop.
Save gladson/98114b2225defe617fb67d12aed1803d to your computer and use it in GitHub Desktop.
Soma + Listas
<?php
class Listar
{
private $getLista;
private $getTotal;
//Getters:
public function getLista() {
return $this->getLista;
}
public function getTotal() {
return $this->getTotal;
}
//Setters:
public function setLista($varLista) {
$this->getLista = $varLista;
}
public function setTotal($varTotal) {
$this->getTotal = $varTotal;
}
function total() {
$lista = $this->getLista();
$total = 0;
foreach ($lista as $objeto) {
$total += $objeto["valor"];
}
$this->setTotal($total);
return $this->getTotal();
}
function removerItemLista($keyRemove) {
$listaVelha = $this->getLista();
$listaNova = [];
foreach ($listaVelha as $key => $value) {
if ($key != $keyRemove) {
$listaNova[] = $value;
}
}
return $listaNova;
}
}
$data = [
[
"id" => 1,
"descricao" => "Item 1",
"valor" => 75.0
],
[
"id" => 2,
"descricao" => "Item 2",
"valor" => 75.0
],
[
"id" => 3,
"descricao" => "Item 3",
"valor" => 125.0
],
[
"id" => 4,
"descricao" => "Item 4",
"valor" => 125.0
]
];
$resultado = new Listar();
$resultado->setLista($data);
// var_dump($resultado->total());
var_dump($resultado->removerItemLista(3));
// print($data[0]["valor"]."\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment