Retornar Array no PHP
Autor: Wesley Ferreira Messias | Publicado: 2017-01-07T09:29:00-08:00

Boa tarde amigos, essa semana estudando PHP tive uma dor de cabeça. Como retornar um array no PHP, vindo de um banco MYSQL, utilizando o foreach, bem melhor mostrar né, segue:

Função criada em um arquivo PHP, ao qual se lê do banco:

function buscar_tarefas($conexao){&nbsp;&nbsp;&nbsp; &nbsp;$sqlBusca = 'SELECT * FROM tarefas';&nbsp;&nbsp;&nbsp; //$resultado = mysqli_query($conexao,$sqlBusca);&nbsp;&nbsp;&nbsp; &nbsp;$resultado = $conexao-&gt;query($sqlBusca);&nbsp;&nbsp;&nbsp; $tarefas = array();&nbsp;&nbsp;&nbsp; while ($tarefa = $resultado-&gt;fetchAll(PDO::FETCH_ASSOC) ){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$tarefas[] = $tarefa;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;return $tarefas;}

PHP index que chama a função:

&lt;?php &nbsp;&nbsp;&nbsp; session_start();&nbsp;&nbsp;&nbsp; require "banco.php";&nbsp;&nbsp;&nbsp; if(array_key_exists('nome',$_GET) &amp;&amp; $_GET['nome'] != ''){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$tarefa = array();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$tarefa['nome'] = $_GET['nome'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['local'] = $_GET['local'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(array_key_exists('descricao', $_GET)){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['descricao'] = $_GET['descricao'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}else{&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['descricao']= '';&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(array_key_exists('prazo', $_GET)){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['prazo'] = $_GET['prazo'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}else{&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['prazo']= '';&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['prioridade'] = $_GET['prioridade'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(array_key_exists('concluida', $_GET)){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['concluida'] = $_GET['concluida'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}else{&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$tarefa['concluida']= '';&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gravar_tarefa($conexao,$tarefa);&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; $lista_tarefas=buscar_tarefas($conexao);&nbsp;&nbsp;&nbsp;&nbsp; include "template.php";?&gt;

Arquivo PHP que recebe a variavel $lista_tarefas:
&nbsp;&nbsp; 


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;?php &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; foreach($lista_tarefas as $linhas=&gt;$rows_1): ?&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;?php foreach($rows_1 as $rows): ?&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;tr&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $rows['id']; ?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $rows['nome']; ?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $rows['descricao'];?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;?php $data = $rows['prazo'];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $dataP = explode('-', $data);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $parte = substr($dataP[2],0,2);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $dataParaExibir = $parte.'/'.$dataP[1].'/'.$dataP[0];?&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $dataParaExibir; ?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $rows['prioridade']; ?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $rows['concluida']; ?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;?php echo $rows['local']; ?&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;/tr&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;?php endforeach; ?&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;?php endforeach; ?&gt;&nbsp;&nbsp;

Ou seja no retorno desta array eu tenho que criar duas foreach para mostrar o conteúdo que desejo.
Bem fica a dica.&nbsp;