Return of an array’s Indice value

Asked

Viewed 33 times

0

I have an array with multiple positions, as I do to check if a particular Dice is numeric or string?

1 answer

0


On the php website itself you have this answer: is_numeric: https://www.php.net/manual/en/function.is-numeric.php is_string: https://www.php.net/manual/en/function.is-string.php

Examples:

// valores de uma array
$a = 2;
echo is_numeric($a);
echo is_string($a);
$arr = array(1, "v", 3,"g");
foreach ($arr as &$value) {
    if (is_numeric($value)) {
        echo "numeric";
    }
    if (is_string($value)) {
       echo "string";
    }
}
// para índices
$produto = [208 => 4, 'Gol' => 84, 'Fox' => 100];
foreach ($produto as $key => $value) {
    if (is_numeric($key)) {
        echo "numeric";
    }
    if (is_string($key)) {
       echo "string";
    }
}

Console:

true
false
numeric
string
numeric
string
numeric
string
string

I hope I’ve helped.

  • That was just an example.

  • Yes, but in my case I don’t want to check the value, it would be the Dice, for example: $product = [208 => 4, 'Gol' => 84, 'Fox' => 100]; Would be.... foreach ($product as $key => $value){ if(is_numeric($key)){ echo "Numeric"; } }

  • I know that. But how do I know which indexes you used?

  • Yes, too much. Thank you very much.

Browser other questions tagged

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