How to assign to a variable the results of an array

Asked

Viewed 181 times

1

I am receiving several times in a function, when calling the function in the Controller use the foreach and wanted these results(times) to be printed on the screen for the user to see, but using the echo I can’t print with just the var_dump, but I want it to appear on the screen only hours without var_dump.

Code of Model:

public function select_horarios_selecionados($data, $horarios=array()){
    if(is_array($horarios)){
        foreach($horarios as $key){

            $this->db->select('hora');
            $this->db->where('dia', $data);
            $this->db->where('hora', $key);
            $query = $this->db->get('agendamentos')->result();
            $horarios_selecionados[] = array();

            if($query){
                foreach($query as $value){
                    $horarios_selecionados[] = $value->hora;                        
                }
            }else{
                return false;
            }
        }

        return $horarios_selecionados;
    }

}

Code of Controller

if($consulta = $this->agenda->select_horarios_selecionados($data, $horarios)){
                foreach($consulta as $linha){
                    $horarios_ocup = $linha;
                    var_dump($horarios_ocup);
                }

            }else{
                echo "Data e horarios diponiveis para agendamento";
            }

RESULT THAT APPEARS ON THE SCREEN:

array(0) { } string(8) "09:00:00" array(0) { } string(8) "09:30:00"

I want to show only the example schedules: 09:00:00 09:30:00

  • uses foreach, checks in the documentation, there are examples of how to view without showing the full array, just with the formatting you want. http://php.net/manual/en/control-structures.foreach.php

  • I’m already using Foreach but it didn’t work

  • Then check this function str_replace, but it will give you more work: http://php.net/manual/en/function.str-replace.php

  • Please I’ve seen this documentation, I need help from someone who can give me an example and a brief explanation, do not need to give the code ready.

  • How do I use echo to see only clear hours 09:00:00 with echo without var_dump? because var_dump has array and array information

  • The right place to give echo wouldn’t be the view instead of the controller? And that’s all the controller code?

  • trial $horarios_selecionados = array(); because here you are creating a list is not? in php you can create a list $list[] as $list = array() in the case as Voce did seems to me one inside the other. I honestly can not explain, because I have not tested here.

  • I’ve done this way Eduardo and it didn’t work. .

  • What appears when you do print_r($line) ?

Show 4 more comments

2 answers

2


Very simple, I will adapt your code by adding comments of some changes I made, see:

Code of Model:

public function select_horarios_selecionados($data, $horarios=array()) {
    if(is_array($horarios)) {

        /* Mudei aqui o lugar aonde estava sendo criado o array,
           se criar dentro do loop foreach você vai perder os dados
           toda vez que reiniciá-lo. E tirei os [] da declaração da variável. */

        $horarios_selecionados = array();

        foreach($horarios as $key){

            $this->db->select('hora');
            $this->db->where('dia', $data);
            $this->db->where('hora', $key);
            $query = $this->db->get('agendamentos')->result();

            if(is_array($query)) {
                foreach($query as $value) {
                    $horarios_selecionados[] = $value->hora;                        
                }
            } else {
                return false;
            }
        }

        return $horarios_selecionados;
    }

}

Code of Controller:

/* Aqui você irá receber um array de strings, basta fazer um foreach
   usando echo para imprimir a variável do laço. Concatenei um espaço
   vazio para não sair todos os horário colados. */

$consulta = $this->agenda->select_horarios_selecionados($data, $horarios);
if(is_array($consulta)){
    foreach($consulta as $linha){
        echo $linha . ' ';
    }
} else {
    echo 'Não foi possível recuperar os horários';
}
  • Thank you Pedro for the help ran straight here, now I will study every line of code, I am a student and I hit myself a lot with arrays, was of great help :)

  • You’re welcome, I’m glad you helped.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.