Retry all values in Array stdClass Object

2

I would like help. I have an Array that returns the following values:

Array
(
   [0] => stdClass Object
      (
         [seccionalid] => 1
         [seccionaldescricao] => DELSECPOL DE SJRIO PRETO
      )

   [1] => stdClass Object
      (
         [seccionalid] => 2
         [seccionaldescricao] => DELSECPOL DE JALES
      )

   [2] => stdClass Object
      (
         [seccionalid] => 3
         [seccionaldescricao] => DELSECPOL DE ARAÇATUBA
      )

I am trying to echo or print_r , return to a function, all values of [seccionaldescricao] of Array , as follows:

$dados = $seccionais->fetchAll(PDO::FETCH_OBJ);
$dados2 = $dados[0]->seccionaldescricao;

print_r($dados2);

However, only one line is returned:

DELSECPOL DE SJRIOPRETO

How do I return all records?

    
asked by anonymous 31.08.2015 / 19:07

4 answers

2

It looks like you need to create a new array with only this data. It can be manually, using a loop, as in André Baill's response, or by using array_map , like this:

function descricoes($item) {
    return $item->seccionaldescricao;
}
$dados2 = array_map(descricoes, $dados);
print_r($dados2);
    
31.08.2015 / 19:29
1

You can elaborate as follows:

$dados = $seccionais->fetchAll(PDO::FETCH_OBJ); 
foreach($dados as $valor){
    echo $valor->seccionaldescricao;
    echo "<br>";    
}

In this way it will search the arrays () that you have assigned and will print according to the field you want.

    
31.08.2015 / 19:13
1

When you access -> you are referencing an attribute with 1 item, to access all, you will need to create a loop:

foreach($seccionais as $result){
    echo($result->seccionaldescricao);
}
  

or

while ($row = $seccionais->fetch(PDO::FETCH_ASSOC))
{
$seccionaldescricao= $row['seccionaldescricao'];
}
    
31.08.2015 / 23:07
1

This is an array of objects, as an example I made the following entry from your collection:

<?php
$myCollection = [
    new stdClass(),
    new stdClass(),
    new stdClass(),
    ];

 //como no seu exemplo, você tem 3 posições do array que cada uma representa 2 atributos de um objeto stdClass 
    $myCollection[0]->seccionalid = 1;
    $myCollection[0]->seccionaldescricao = 'DELSECPOL DE SJRIO PRETO';

    $myCollection[1]->seccionalid = 2;
    $myCollection[1]->seccionaldescricao = 'DELSECPOL DE JALES';

    $myCollection[2]->seccionalid = 3;
    $myCollection[2]->seccionaldescricao = 'DELSECPOL DE ARAÇATUBA';

echo "<pre>";
print_r($myCollection);

class PHPIterator implements Iterator
    {
        private $collection = [];
        private $key        = 0;

        public function __construct(array $collection = [])
        {
            $this->collection = $collection;
        }

        public function rewind()
        {
            $this->key = 0;
        }

        public function current()
        {
            return $this->collection[$this->key];
        }

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

        public function next()
        {
            ++$this->key;
        }

        public function valid()
        {
            return isset($this->collection[$this->key]);
        }
    }

    //aqui você faz a interação:
    $phpIterator = new PHPIterator($myCollection);

    //abaixo eu faço com três casos de interação
    echo "-----------------while--------------------\n";

     $phpIterator->rewind();

    while ($phpIterator->valid()) {

        echo $phpIterator->current()->seccionalid."\n";
        echo $phpIterator->current()->seccionaldescricao."\n";
        $phpIterator->next();
    }

    echo "-------------------for--------------------\n";


    for ($phpIterator->rewind(); $phpIterator->valid(); $phpIterator->next()) {
        echo $phpIterator->current()->seccionalid."\n";
        echo $phpIterator->current()->seccionaldescricao."\n";
    }


    echo "------------------foreach-----------------\n";

    foreach ($phpIterator as $key => $object) {
       echo $object->seccionalid."\n";
       echo $object->seccionaldescricao."\n";
    }

The PHP documentation has more information about this design pattern: link

Now using a more basic form, no design pattern, you can do it this way:

    //Suponha que sua coleção seja essa:
            $myCollection = [
            new stdClass(),
            new stdClass(),
            new stdClass(),
            ];

         //como no seu exemplo, você tem 3 posições do array que cada uma representa 2 atributos de um objeto stdClass 
            $myCollection[0]->seccionalid = 1;
            $myCollection[0]->seccionaldescricao = 'DELSECPOL DE SJRIO PRETO';

            $myCollection[1]->seccionalid = 2;
            $myCollection[1]->seccionaldescricao = 'DELSECPOL DE JALES';

            $myCollection[2]->seccionalid = 3;
            $myCollection[2]->seccionaldescricao = 'DELSECPOL DE ARAÇATUBA';
//lembrando que a variável $myCollection é uma simples representação do retorno de: $dados = $seccionais->fetchAll(PDO::FETCH_OBJ);

if (count($myCollection)) {
    foreach($myCollection as $data) {
       echo $data->seccionalid.'<br>';
       echo $data->seccionaldescricao.'<br>';
    }
}

Here's the example that works: link

    
31.08.2015 / 22:48