Problems with an associative Array in php

0

I have a book class that has among its attributes an array where the records of each loan that is made of the book (reader code, loan date, estimated return date and real return date) must be kept.
To do this, I first use the prestar() function that creates an associative array for each parameter in the last position of the same, thus giving rise to a bidimensional array of loan records. The function prestar() fills in all the fields of the loan record, except the last Freal that must be filled by the function devolver() and that's where the problem is, I use the function end() to access the last field of the array but although I do not get an error and even doing echo you can see that access to that last position does not fill in the field I'm missing.

final class libro extends publicacion implements prestable{
    private $prestado = false;
    private $ArrayPrestamos = array();

    public function __get($atributo) {
        if(property_exists(__CLASS__, $atributo)){
            return $this->$atributo;
        } else {
            return null;
        }
    }

    public function __set($nombre, $valor) {
        $this->$nombre=$valor;
    }

    public function __toString(){
        return parent::__toString()." | ¿Prestado? ".(boolval($this->prestado) ? 'Si' : 'No');
    }

    public function prestar($codLector) {
        try{
            if(!$this->prestado) {
                $f_actual = date('d-m-Y');
                $f_stimada = strtotime('+10 day' , strtotime($f_actual));
                $f_stimada = date('d-m-Y' , $f_stimada);
                $this->ArrayPrestamos[] = array ("codL"=>$codLector, "Fprestamo"=>$f_actual, "Fstimada"=>$f_stimada, "Freal"=>'');
                $this->prestado = true;
            } else {
                throw new InvalidArgumentException("ERROR: El libro ya esta prestado");
             } 
        }
        catch(InvalidArgumentException $e){
            echo $e->getMessage();
        }   

    }

    public function devolver() {
        try{
            if($this->prestado) {
                $f_actual = date('d-m-Y');
                end($this->ArrayPrestamos)["Freal"] = $f_actual;
                $this->prestado = false;
            } else {
                throw new InvalidArgumentException("ERROR: El libro no esta prestado");
             } 
        }
        catch(InvalidArgumentException $e) {
            echo $e->getMessage();
        }
    }

    public function EstaPrestado() {
        return $this->prestado;
    }

    public function MuestraArray() {
        return var_dump($this->ArrayPrestamos); 
    }


}  

And here is the proof of execution:

echo "<p>--- Creamos el libro \"Titulo Libro 1\"</p>";
$book1 = new libro (4, "Titulo Libro 1", 1997);
$book1->prestar(44);
$book1->devolver();
echo "<p>".$book1->MuestraArray()."</p>";
echo $book1;
    
asked by ivanao 05.03.2018 в 01:46
source

2 answers

1

Because the function end returns the value of the last element, as the documentation says, if you return the reference to the last element you could do it as you wrote it, try it in the following way

end($this->ArrayPrestamos);
$key = key($this->ArrayPrestamos);
$this->ArrayPrestamos[$key]["Freal"] = $f_actual;

or, also

$ultimo = array_pop($this->ArrayPrestamos);
$ultimo["Freal"] = $f_actual;
$this->ArrayPrestamos[] = $ultimo;
 // o
array_push($this->ArrayPrestamos, $ultimo);
    
answered by 05.03.2018 / 02:13
source
1

What happens is that with the function end you can not make changes on the return value. For that you have to access the element through its password. Something like that, to take the last one from the array:

$this->ArrayPrestamos[count($this->ArrayPrestamos) - 1]["Freal"] = $f_actual;
    
answered by 05.03.2018 в 02:17