Check if there is value of an array element

Asked

Viewed 134 times

0

Guys, I’ve put together the following code to read a array:

    for ($i_p = 1; $i_p < $array; $i_p++) {

        // Pega nome e valos da variavel
        $array_v_p = explode("=", $array_p[$i_p]);

        // Verifica se existe valor para montar variáveis
        if (empty($array_v_p[0]) !== (string) null) {

            if (empty($array_v_p[1]) !== (string) null) {
                $variavel = $array_v_p[1];
            } else {
                $variavel = null;
            }

            $$array_v_p[0] = "$array_v_p[1]";
        }
    }

The following error is shown::

PHP Notice: Undefined offset: 1 in index.php on line 8

That’s the line that’s wrong:

if (empty($array_v_p[1]) !== (string) null) {

Does anyone know how to treat that mistake?

1 answer

1


Change:

if (empty($array_v_p[1]) !== (string) null)

For:

if ( isset($array_v_p[1]) and !empty($array_v_p[1]) )

That is, if the element exists AND if it is not empty.

  • OK vlw, it worked out here :)

Browser other questions tagged

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