Search code examples
phparrayszend-frameworkzend-dbzend-db-table

Print array into another array [Zend]


I use Zend 1

CONTROLLER:

    public function insertarAction() {

        $entidades_salud = new Application_Model_DbTable_EntidadesSalud();
        $datos_entidades = $entidades_salud->buscarEntidades();
}

MODEL:

<?php

class Application_Model_DbTable_EntidadesSalud extends Zend_Db_Table_Abstract
{

    protected $_name = 'entidades_salud';
    protected $_primary = 'codigo_entidad_salud';

    public function buscarEntidades() {

        $consulta = $this->select()->from($this->_name);
        $query = $this->fetchAll($consulta)->toArray();
        return $query;

    }


}

PRINT OF CONSULT [print_r($datos_entidades); - in controller]

    Array ( [0] => Array ( [codigo_entidad_salud] => 1 [nombre_entidad] => SANITAS ) 
             [1] => Array ( [codigo_entidad_salud] => 3 [nombre_entidad] => wladfimir )
             [2] => Array ( [codigo_entidad_salud] => 4 [nombre_entidad] => Juli ))
no entraArray ( [controller] => beneficiarios [action] => insertar [module] => default ) 

According to the above result, I require print:

1 SANITAS
3 wladfimir
4 Juli

I think it should work with something like:

        while($row = mysqli_fetch_assoc($datos_entidades)){
                echo $row['codigo_entidad_salud'];
        echo $row['nombre_entidad'];
}

Solution

  • You are converting The results into array, you can do what Rikesh Suggested as,

    foreach($datos_entidades as $row){
        echo $row['codigo_entidad_salud'];
        echo $row['nombre_entidad'];
    }
    

    And you can pass $datos_entidades to View Script as $this->view->data= $datos_entidades;

    and you will be able to use,

    foreach($this->data as $row){
        echo $row['codigo_entidad_salud'];
        echo $row['nombre_entidad'];
    }
    

    in your view script also,

    an alternate approach,

    You dont use toArray() in $query = $this->fetchAll($consulta)->toArray();

    just use, $query = $this->fetchAll($consulta);

    and you will be able access it like following,

    foreach($datos_entidades as $row){
        echo $row->codigo_entidad_salud;
        echo $row->nombre_entidad;
    }
    

    and will be able to access same way in view script also.