使用foreach连接字符串

I'm trying to make a generated random characters. I'm concatenating the values of $char_type using foreach loop but it doesn't show anything. Here's my code:

    public function randomizer($range, $type) {
     $strtester = '';
     $char_type = array('alp_sm' => 'abcdefghijklmnopqrstuvwxyz',
                        'alp_cs' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                        'num'    => '0123456789',
                        'sp'     => '!@#$%^&*()');

     if(is_array($type)) {
         foreach($type as $row) {
             if(in_array($row, $char_type)) {
                 $strtester .= $char_type[$row];
             }
         }
     }
     print_r($strtester); exit();
     $seed = str_split($strtester);
     shuffle($seed);
     $generated_string = '';
     foreach (array_rand($seed, $range) as $k) $generated_string .= $seed[$k];

     return $generated_string;
    }

Update: What i want to get from $strtester is for example I want $char_type alp_sm and alp_cs then the $strtester will get abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Thank you in advance

Replace if(in_array($row, $char_type)) { to if(array_key_exists($row, $char_type)) { and then try

    function randomizer($range, $type) {
         $strtester = '';
         $char_type = array('alp_sm' => 'abcdefghijklmnopqrstuvwxyz',
                            'alp_cs' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                            'num'    => '0123456789',
                            'sp'     => '!@#$%^&*()');

        if(is_array($type)) {
            foreach($type as $row) {
                if(array_key_exists($row, $char_type)) {
                    $strtester .= $char_type[$row];
                }
            }
        }

        $seed = str_split($strtester);
        shuffle($seed);
        $generated_string = '';
        foreach (array_rand($seed, $range) as $k) 
            $generated_string .= $seed[$k];
        return $generated_string;
    }

    $res =  randomizer(5, array("alp_sm"));
    print_r($res);

The error is here:

if(in_array($row, $char_type)) {

You're actually looking for if the key exists, not whether the array contains that string - you need to use isset:

if(is_array($type)) {
    foreach($type as $row) {
        if(isset($char_type[$row]) {
            $strtester .= $char_type[$row];
        }
    }
}
print_r($strtester); exit();

You have to check for is_array. It checks whether the variable is an array

$strtester = '';
if(is_array($type)) {
    foreach($type as $row) {
        if(isset($char_type[$row]) {
            $strtester .= $char_type[$row];
        }
    }
}

refer the PHP Docs for is_array

I'm not sure about this but I think this might be the easiest one to work around

function randomizer($range, $type) {
     $char_type = array('alp_sm' => range('a','z'),
                        'alp_cs' => range('A','Z'),
                        'num'    => range(0,9),
                        'sp'     => ['!','@','#','$','%','^','&','*','(',')']);
    shuffle($char_type[$type[0]]);                    
    $generated_string = implode(array_slice($char_type[$type[0]],0,$range));
    return $generated_string;
}

$res =  randomizer(5, array("alp_sm"));
print_r($res);