Last active
September 4, 2020 22:05
-
-
Save gladson/98114b2225defe617fb67d12aed1803d to your computer and use it in GitHub Desktop.
Soma + Listas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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