Look for numbers in the multidimensional array (multiple layers)

Asked

Viewed 190 times

0

I would like to search for a certain number in this array ($arrayJobsAtivos):

Array
(
    [0] => Array
        (
            [seg] => Array
                (
                    [manha] => 1
                    [tarde] => 1
                )

            [ter] => Array
                (
                    [manha] => 1
                    [tarde] => 1,2
                )

            [qua] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [qui] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [sex] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

        )

    [1] => Array
        (
            [seg] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [ter] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [qua] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [qui] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [sex] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

        )

    [2] => Array
        (
            [seg] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [ter] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [qua] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [qui] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

            [sex] => Array
                (
                    [manha] => 
                    [tarde] => 
                )

        )

)

I am trying this way. If you find, $jobUso is "green". If not, "red". However, I think that being multidimensional, I’m not finding the values, because it’s always falling in the red.

One thing... if the index has more than one number (e.g.: 1.2), it should also have green... then I believe that "in_array" is not interesting, correct?

$jobUso = ((array_search(1, array_column($arrayJobsAtivos, 'manha')) !== false) || (array_search(1, array_column($arrayJobsAtivos, 'tarde')) !== false)) ? "verde" : "vermelho";

EDIT

I did so below, and it works. However, I would like to know if there is a shorter and smarter option.

for($i = 0; $i < count($arrayJobsAtivos); $i++){

    // * Segundo nível - Dias da semana
    foreach($arrayJobsAtivos[$i] as $key => $value){

        // * Terceiro nível - Período do dia
        foreach($arrayJobsAtivos[$i][$key] as $periodo => $jobs){

            // * Explodimos a string porque pode haver mais de um job no período
            $jobsDoDia = explode(",",$jobs);

            // * Com isso, um array é gerado. Agora entramos num loop deste array para verificar se o ponteiro do array é igual ao do job vigente da lista
            for($j = 0; $j < count($jobsDoDia); $j++){

                if($jobsDoDia[$j] == 1){
                    $jobUso = "verde";
                }
            }
        }
    }
}
  • The in_array seems to work. I didn’t quite get that last paragraph.

  • @Francisco what he meant is that, for example, see the index [0]['ter']['tarde']. See that it has the string "1,2". If I search with in_array looking for item "1", it will not find in this case. It will only bring index [0]['seg']['manha'] and [0]['seg']['tarde'].

No answers

Browser other questions tagged

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