0

I have this problem. In plain PHP, this function is working perfectly and returns correct result:

$focos_3[ (array_search("$c", $cidades_3) ? array_search("$c", $cidades_3) : ('') ) ]

OBS: sometimes $c is not set.

But when I put it in Laravel its stops working and throw me "Undefined index:". Without index indication.

Doing some tests, I found that if I do this:

array_search("$c", $cidades_3)

or this

in_array("$c", $cidades_3)

works perfectly, even if $c is not set.

Some answers:

$cidades_3 starts at index 1 $focos_3 is array of values that will return a result on position that was requested.

The whole function which I need to get working is this:

foreach ($cidades as $c) 
    {
        $registros = array(
            "Estado"    => $estados[$c],
            "Municipio" => $c, 
            "tres_dias" => $focos_3[ (array_search("$c", $cidades_3) >=0 ? array_search("$c", $cidades_3) : ('') ) ], 
            "dois_dias" => $focos_2[ (array_search("$c", $cidades_2) >=0 ? array_search("$c", $cidades_2) : ('') ) ], 
            "um_dia"    => $focos_1[ (array_search("$c", $cidades_1) >=0 ? array_search("$c", $cidades_1) : ('') ) ], 
            "total"     => (
                                         ($focos_3[ (array_search("$c", $cidades_3) >=0 ? array_search("$c", $cidades_3) : ('') ) ]) + 
                                         ($focos_2[ (array_search("$c", $cidades_2) >=0 ? array_search("$c", $cidades_2) : ('') ) ]) + 
                                         ($focos_1[ (array_search("$c", $cidades_1) >=0 ? array_search("$c", $cidades_1) : ('') ) ])
                                         ) 
            );
    }

And yes, its similar problem to this question (Laravel breaks entire app on PHP notices), but there we have not very goo solution (suppression error).

Community
  • 1
  • 1
Igor Kolesnikov
  • 133
  • 1
  • 8
  • This seems to be an issue related to error reporting options. Check this out: http://stackoverflow.com/questions/18497788/laravel-breaks-entire-app-on-php-notices – Gustavo Straube Apr 10 '17 at 12:54
  • What is supposed to happen if $c is found at index 0 in $cidades_3? Or if the value of $c is 'x' and the array contains an entry with the value 0 (or vice versa)? – Mark Baker Apr 10 '17 at 12:54
  • Possible duplicate of [Laravel breaks entire app on PHP notices](http://stackoverflow.com/questions/18497788/laravel-breaks-entire-app-on-php-notices) – Gustavo Straube Apr 10 '17 at 12:54
  • How is `$focos_3` used? The error states that the index `''` is not defined, so that is not related to the `array_search` in your ternary operator, but to the index `''` on (most likely) the `$focos_3` array. – Robert Apr 10 '17 at 13:03
  • If array search returns 0 (which may be valid) you'll get `''` returned by the ternary operator which is probably undefined. An undefined index is a warning which PHP usually swallows or writes somewhere, but Laravel treats as an unhandled exception. – apokryfos Apr 10 '17 at 13:04

2 Answers2

0

Use if is set

if(isset($var){
//execute function
}
Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
-2

Indeed, in this case it was easier to suppress warning message with Laravel. Putting an '@' here solved error.

foreach ($cidades as $c) 
{
    @$registros = array(
        "Estado"    => $estados[$c],
        "Municipio" => $c, 
        "tres_dias" => $focos_3[ (array_search("$c", $cidades_3) >=0 ? array_search("$c", $cidades_3) : ('') ) ], 
        "dois_dias" => $focos_2[ (array_search("$c", $cidades_2) >=0 ? array_search("$c", $cidades_2) : ('') ) ], 
        "um_dia"    => $focos_1[ (array_search("$c", $cidades_1) >=0 ? array_search("$c", $cidades_1) : ('') ) ], 
        "total"     => (
                                     ($focos_3[ (array_search("$c", $cidades_3) >=0 ? array_search("$c", $cidades_3) : ('') ) ]) + 
                                     ($focos_2[ (array_search("$c", $cidades_2) >=0 ? array_search("$c", $cidades_2) : ('') ) ]) + 
                                     ($focos_1[ (array_search("$c", $cidades_1) >=0 ? array_search("$c", $cidades_1) : ('') ) ])
                                     ) 
        );
}
Igor Kolesnikov
  • 133
  • 1
  • 8