Taking PHP array values

Asked

Viewed 90 times

0

Good afternoon. How do I pick up only an array that contains my condition? I’m redoing the question poís I still can’t, but I’m putting all my code.

<?php 
            include 'inc/cabecalho.php';
            include 'inc/menu.php'; 
            include 'script/script.php';
            $e = new Script(); 

            foreach ($e->listarLivros() as $livro){ 

             $a = 0;

             foreach ($e->listarAutores() as $autor){
              $a = $autor['idAutor'];
            }
            ?>

            <div class="col-md-4">
              <div class="card card-product card-plain no-shadow" data-colored-shadow="false">
                <div class="card-header card-header-image">
                  <a href="product.html">
                    <img src="<?=$livro['fotoCapa']?>" style="width: 260px; height: 350px;">
                  </a>
                </div>
                <div class="card-body text-center">
                  <a href="#">
                    <h4 class="card-title"><?=$livro['titulo']?></h4>
                  </a>
                  <p class="description">
                    <?=$a?>
                  </p>
                  <p class="description">
                    <?=$livro['id_editora']?>
                  </p>
                </div>
              </div>
            </div>

          <?php }  ?> 

Below would be where I get the data in a class -

    <?php 

class Script
{   
    public $editora     = array();
    public $autor       = array();
    public $livros      = array();
    public $base_livros = base_livros;
    public $base_autor  = base_autor;

    public function listarEditoras()
    {
        $this->editora  = array
        (
            '1' => array
            (
                'idEditora' => '1', 
                'editora'   => 'Editora Viseu', 
                'endereco'  => 'Av. Duque de Caxias, 882, Maringá - PR', 
                'email'     => '[email protected]', 
            ),  
            '2' => array
            (
                'idEditora' => '2', 
                'editora'   => 'Editora Harper Colllins', 
                'endereco'  => 'Av. Duque de Caxias, 882, Maringá - PR', 
                'email'     => '[email protected]', 
            ),  
            '3' => array
            (
                'idEditora' => '3', 
                'editora'   => 'Editora Schoba', 
                'endereco'  => 'Av. Brg. Faria Lima, 1811 - 918, São Paulo - SP', 
                'email'     => '[email protected]', 
            ),
        );

        return $this->editora;
    }

    public function listarAutores()
    {
        $this->autor  = array
        (
            '1' => array
            (
                'idAutor'  => '1', 
                'autor'    => 'Oswaldo Tognetta', 
                'formacao' => 'Contador', 
                'foto'     => $this->base_autor.'oswaldo_tognetta.jpg', 
            ),  
            '2' => array
            (
                'idAutor'  => '2', 
                'autor'    => 'Josué Ribeiro', 
                'formacao' => 'Segurança do Trabalho', 
                'foto'     => $this->base_autor.'josue_ribeiro.jpg', 
            ),  
            '3' => array
            (
                'idAutor'  => '3', 
                'autor'    => 'Edson Reinaldo', 
                'formacao' => 'Metalúrgico', 
                'foto'     => $this->base_autor.'edson_reinaldo.jpg', 
            ),          
            '4' => array
            (
                'idAutor'  => '4', 
                'autor'    => 'Lisa Genova', 
                'formacao' => 'Oradora', 
                'foto'     => $this->base_autor.'lisa_genova.png', 
            ),          
            '5' => array
            (
                'idAutor'  => '5', 
                'autor'    => 'Dave Asprey', 
                'formacao' => 'Empreendedor', 
                'foto'     => $this->base_autor.'dave_asprey.jpg', 
            ),          
            '6' => array
            (
                'idAutor'  => '6', 
                'autor'    => 'Maureen Johnson', 
                'formacao' => 'Escritora', 
                'foto'     => $this->base_autor.'maureen_johnson.jpg', 
            ),          
            '7' => array
            (
                'idAutor'  => '7', 
                'autor'    => 'Amauri Salvador', 
                'formacao' => 'Letras', 
                'foto'     => $this->base_autor.'amauri_salvador.jpg', 
            ),          
            '8' => array
            (
                'idAutor'  => '8', 
                'autor'    => 'Jucelino de Sales', 
                'formacao' => 'Letras', 
                'foto'     => $this->base_autor.'jucelino_de_sales.jpg', 
            ),
        );

        return $this->autor;
    }

    public function listarLivros()
    {
        $this->livros  = array
        (
            '1' => array
            (
                'idLivro'  => '1', 
                'titulo'    => 'Uma venda na beira da estrada', 
                'totalPaginas' => '258', 
                'edicao' => '1',
                'isbn' => '9788530006358',
                'ano' => '2019',
                'fotoCapa' => $this->base_livros.'uma_venda_na_beira_da_estrada.png', 
                'id_editora' => '1',
                'id_autor' => '1',
            ),          
            '2' => array
            (
                'idLivro'  => '2', 
                'titulo'    => 'Memórias de um contabilista', 
                'totalPaginas' => '542', 
                'edicao' => '1',
                'isbn' => '9788530006556',
                'ano' => '2019',
                'fotoCapa' => $this->base_livros.'memorias_de_um_contabilista.jpg', 
                'id_editora' => '1',
                'id_autor' => '1',
            ),          
            '3' => array
            (
                'idLivro'  => '3', 
                'titulo'    => 'Depressão profunda sobrevivi!', 
                'totalPaginas' => '116', 
                'edicao' => '1',
                'isbn' => '9788554549756',
                'ano' => '2018',
                'fotoCapa' => $this->base_livros.'depressao_profunda_sobrevivi.jpg', 
                'id_editora' => '1',
                'id_autor' => '2',
            ),
        );

        return $this->livros;       
    }
}

What I need is this. When the id_author is equal to the idAuthor get his name. But I’m not getting it via foreach.

  • In this case I always put the idEditor as the key of the array to be able to search. See if it is possible to change the structure of your array.

  • I get it. I’m going to test all the options.

2 answers

4

You can use the array_search with array_column.

the array_column will separate the idEditor values,

and the array_search will take the Dice of the array you are looking for

$key = array_search('1', array_column($editoras, 'idEditora'));
$result = $array[$key];

If you want more than one record of the array, you can use the array_filter

$result = array_filter($editoras, function ($row) {
    return $row['idEditora'] == '1';
});
  • I didn’t know the functions, I’ll test them

  • Yes I would like all fields of the array whose id is 1. That’s kind of emulating a database table. It would be a job where they asked all records to be in array. After this 'table' I will have others in relation to this, for example in the other table of books will have the author, then when listing I want to take this information from the publisher whose id was in the book.

2

Use a foreach to scan the array and an if to check each array within the larger array

foreach ($editora as $editora) {

    //substitua o '1' pelo valor que achar melhor
    if($editora['idEditora'] == '1'){
        var_dump($editora);
    }

}

Browser other questions tagged

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