Separate elements of a vector according to its type

0

I need to separate the elements of a array according to whether it is number , text and alphanumeric but the alphanumerics are not printed: (

<?php
$d[0] = 13;
$d[1] = 133;
$d[2] = 45;
$d[3] = "Hice 89 lagartijas";
$d[4] = 778;
$d[5] = 67;
$d[6] = "Que onda";
$d[7] = 456;
$d[8] = 34;
$d[9] = 645;

$indiceNumeros = 0;
$indiceLetras = 0;
$indiceAlfaNumericos = 0;
for($i=0; $i<=9; $i++){
  switch($d){
      case is_numeric($d[$i]):            
          $s[$indiceNumeros] = $d[$i];
          $indiceNumeros++;
          break;
      case is_string($d[$i]):
          $k[$indiceLetras] = $d[$i];
          $indiceLetras++;
          break;
       case ctype_alpha($d[$i]):
           $a[$indiceAlfaNumericos] = $d[$i];
           $indiceAlfaNumericos++;
           break;
       
  }
}
echo "Numeros <BR>";

for($i = 0; $i<count($s); $i++){
  echo "$s[$i] <br>";
}
echo "<br> Textos <BR>";

for($i = 0; $i<count($k); $i++){
  echo "$k[$i] <br>";
}
echo "<br> Alfanuméricos <BR>";
for($i = 0; $i<count($a); $i++){
  echo "$a[$i] <br>";
}
    
asked by Esteban Vera 25.02.2017 в 05:24
source

1 answer

1

Greetings, the problem happens because of the following:

Texts like alphanumeric texts when going to the is_string function will return true. That is why in none of the cases does it reach the third case.

Another point to consider is that the function ctype_alpha is used to determine if all the elements of a text string are letters of the alphabet, from A to z.

So the second case you can place that function to count the texts and the third case place the negation of ctype_alpha (! ctype_alpha) to count the values with letters and numbers.

Sorry to not put code but I'm from the phone

    
answered by 25.02.2017 / 07:41
source