Using array search in a php multidimensional array

In a list of books within a multidimensional array, each sub_array has a different column category , I would like to search in that array for a category for example:

Array  "livros"
    (
        [Livro 1] => Array
            (
                [titulo] => Bleach
                [resumo] => xxx
                [categoria] => Ação, Comédia...
            )

        [Livro 2] => Array
            (
                [titulo] => Titulo livro 2
                [resumo] => xxx
                [categoria] => Ação, Psicológico, Romance ...
            )

        [livro 3] => Array
            (
                [titulo] => Titulo do livro 3
                [resumo] => xxx
                [categoria] => Romance, Vampiros ...
            )
)

I would like to research for example which books fit in the "Novel" category, I did some research and found what might be the solution, but for some reason it's not working, which would be this function:

$romances = array_keys(array_column($livros, 'categoria'), 'Romance');

The above function does not bring no value, just an empty array, What am I doing wrong?

Author: novic, 2016-11-18

2 answers

Combine array_keys com array_filter, and in comparison with strpos which searches for the first occurrence of a text (string).

Note: if you want case-insensitive, change the function of strpos for stripos which by definition: finds the first occurrence of a string case-insensitive (PHP site stripos ).

based on Soen's answer

$livros = array(
    'Livro 1' => array (
        'titulo' => 'Bleach',
        'resumo' => 'xxx',
        'categoria' => 'Ação, Comédia...'
    ),
    'Livro 2' => array (
        'titulo' => 'Titulo livro 2',
        'resumo' => 'xxx',
        'categoria' => 'Ação, Psicológico, Romance ...'
    ),
    'Livro 3' => array (
        'titulo' => 'Titulo do livro 3',
        'resumo' => 'xxx',
        'categoria' => 'Romance'
    )
);

$search = 'Romance';
$romances = array_keys(
    array_filter(
        $livros,
        function ($value) use ($search) {
            return (strpos($value['categoria'], $search) !== false);
        }
    )
);

Online Example

references:

 2
Author: novic, 2017-05-23 12:37:30

This happens because in the case of the question there is no book whose category is only "novel", otherwise they would return data. We have to check if in the string $livros[x]['categoria'] there is the word "Romance", do the following:

$livros = array(
    'livro 1' => array(
        'titulo' => 'Bleach',
        'resumo' => 'xxx',
        'categoria' => 'Ação, Comédia'
    ),
    'livro 2' => array(
        'titulo' => 'Titulo livro 2',
        'resumo' => 'xxx',
        'categoria' => 'Ação, Psicológico, Romance...'
    ),
    'livro 3' => array(
        'titulo' => 'Titulo livro 3',
        'resumo' => 'xxx',
        'categoria' => 'romance, Vampiros ...'
    )
);
$romances = array();
foreach($livros as $livro => $data) {
    if(stripos($data['categoria'], 'Romance') !== false) {
        $romances[] = $livro; 
    }
}
print_r($romances);

Output of $romances (the array with the names of books that are also novels):

Array ([0] = > Book 2 [1] => Book 3)

Demo

 4
Author: Miguel, 2016-11-19 00:29:17