第三十七套

1

//生成随机字符串
$s='a';
for($i=0;$i<15;$i++)
{
    if(rand(0,2)>1)
    {
        $s.=chr(rand(48, 57));
    }
    else
    {
        $s.=chr(rand(97, 122));
    }
}

echo $s.'   ';

自己写的答案,觉得代码有些繁杂,望谅解!

for ($i=0; $i $s) ; $i++) { 
    $x = substr($s,$i,1);
    $y = ord($x);
    if($i ==strlen($s)-1){
        if(ord(substr($s,$i,1))>=48&&ord(substr($s,$i,1))<=57){
            $left[]=substr($s,$i,1);
        }else{
            $right[]=substr($s,$i,1);
        }
    }
    if($y>=48&&$y<=57){
        for ($j=$i+1; $j $s) ; $j++) { 
            if(ord(substr($s,$j,1))>=97&&ord(substr($s,$j,1))<=122){
                $left[] =substr($s,$i,$j-$i);
                $i =$j-1;
                break;
            }
            if($j==strlen($s)-1){
                $left[] =substr($s,$i,$j-$i+1);
                break 2;
            }
        }

    }else if($y>=97&&$y<=122){
        for ($j=$i+1; $j $s) ; $j++) { 
            if(ord(substr($s,$j,1))>=48&&ord(substr($s,$j,1))<=57){
                $right[] =substr($s,$i,$j-$i);
                $i =$j-1;
                break;
            }
            if($j==strlen($s)-1){
                $right[] =substr($s,$i,$j-$i+1);
                break 2;
            }

        }

    }
}
//合并两个数组一个做键一个做值
$array_ab=array_combine($right,$left);
echo "
";
var_dump($array_ab);
echo "
"
;

2.

$T = array();
for ($i = 0; $i < 100; $i ++){
    $s = array();
    for ($n = 0; $n < rand(1, 9); $n ++){
        $s[] = '[' . rand(1, 9) . ']';
    }
    for ($j = 0; $j < rand(1, 9); $j ++){
        @eval('$T' . implode('', $s) . '[' . $j . ']' . '=' . rand(1, 10000) . ';');
    }
}
echo '
';
print_r($T);

百度搜索的答案

function arr_foreach ($arr)
   {
      static $tmp=array(); 
      if (!is_array ($arr))
      {
         return false;
      }
      foreach ($arr as $val )
      {
         if (is_array ($val))
         {
            arr_foreach ($val);
         }
         else
         {
            $tmp[]=$val;
         }
      }
      return $tmp;
   }
echo '
';
print_r(arr_foreach($T));
echo "
"
;

你可能感兴趣的:(PHP-面试题总集)