Look up numbers in the multidimensional array (multiple layers)

0

I would like to look up a certain number in this array ($ arrayJobsActive):

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'm trying this way. If it finds, $ jobUse is "green". If not, "red". However, I think because it is multidimensional, I am not finding the values, because it is always falling in the red.

One thing ... if the index has more than one number (ex: 1,2), it should also include the green ... so 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've done it this way, 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";
                }
            }
        }
    }
}
    
asked by anonymous 22.01.2018 / 21:37

0 answers