Use array_search in a php multidimensional array

Asked

Viewed 6,203 times

3

In a list of books within a array multidimensional, each sub_array has a column category different, I would like to search in this array by 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 category "Romance", I did some research and found what might be the solution, but for some reason is not working, that would be this function:

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

The above function brings no value, only one array empty, what I’m doing wrong?

2 answers

4

This is because in the case of the question there is no book whose category is ONLY "Romance", but would return data. We have to check 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 from $romances (the array with the names of books that are also novels):

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

DEMONSTRATION

  • The problem is that this function will already be executed, within another foreach, because I have an array with the categories and each time it goes to fetch all books in this category, it is not heavy one foreach within another?

  • It gives anyway, Leo. You could use a native function that you would give anyway because an iteration is also done on the array.

  • There is nothing to do one way or another (natively or not) will have to happen

  • I think with stripos() could dispense with the strtolower()

  • I tested this @Danielomine, http://ideone.com/xmvJHL, can’t, is case sensitive

  • The strpos() is different from stripos(). Note that you have an "I" of "insensitive.

  • Ha is right, it’s true I forgot this @Danielomine, thanks I will edit

  • Personal vlw worked really well, I was breaking my head here with this kk

  • :) good thing you solved @Leoletto. You’re welcome

Show 4 more comments

2


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

Observing: If you don’t want to differentiate between upper and lower case, change the function of strpos for strips that by definition: finds the first occurrence of a string without differentiating between upper and lower case (PHP strip site).

Based on the response of Soen

$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:

  • 1

    Worked out the way I need to vlw man

Browser other questions tagged

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