Random phone number xxx or yyy + operator number mm or nn + a random 6 or 7 digit number in php -


i need code generate random phone number , if chooses 331 prefix has have 2x operator numbers, if chooses 333 has contain 5x operator numbers, , @ last random number, if it's 381 it's 7 digit number , if 389 it's 6 digit number have done this, @ end doesn't print out anything.

<?php         //an array containing prefixes         $string_preprefix = array('a' => '331', 'b' => '333',);         shuffle($string_preprefix);         reset($string_preprefix);          array containing operator numbers 389 prefix         $string_prefix = array('20', '22', '23', '24', '26', '28', '29',);         shuffle($string_prefix);         reset($string_prefix);          //an array containing operator numbers 381 prefix         $string_prefix = array('53', '54', '55', );         shuffle($string_prefix);         reset($string_prefix);          //a function generates random number         function generaterandomnumber($length = 6) {             $number = '1234567890';             $numberlength = strlen($number);             $randomnumber = '';             ($i = 0; $i < $length; $i++) {                 $randomnumber .= $number[rand(0, $numberlength - 1)];             }             return $randomnumber;         }          //the if/elseif method doesn't print         if ($string_preprefix == ['a']):             echo $string_preprefix . $string_prefix . generaterandomnumber(6);         elseif($string_preprefix == ['b']):             echo $string_preprefix . $string_prefix . generaterandomnumber(7);         endif;         ?> 

you have couple of issues. first, if reset arrays before echo them same result each time.

next, need test key of element in first position of array against string, 'a', not ['a'] second example never match unless key has brackets in it.

lastly, have return element in first position of subsequent array has been shuffled. have 2 arrays same name , $string_prefix - second overwrites first. need additional logic make decision there:

//an array containing prefixes $string_preprefix = array('a' => '331', 'b' => '333',); shuffle($string_preprefix); //reset($string_preprefix);  if('389' == $string_preprefix[0]) { // value not in original array     //an array containing operator numbers 389 prefix     $string_prefix = array('20', '22', '23', '24', '26', '28', '29',);     shuffle($string_prefix);     //reset($string_prefix); } elseif ('381' == $string_preprefix[0]) { // value not in original array     //an array containing operator numbers 381 prefix     $string_prefix = array('53', '54', '55', );     shuffle($string_prefix);     //reset($string_prefix); } else {     $string_prefix = '0000'; }  //a function generates random number function generaterandomnumber($length = 6) {     $number = '1234567890';     $numberlength = strlen($number);     $randomnumber = '';     ($i = 0; $i < $length; $i++) {         $randomnumber .= $number[rand(0, $numberlength - 1)];     }     return $randomnumber; }  //the if/elseif method doesn't print if (key($string_preprefix) == 'a'):     echo $string_preprefix[0] . $string_prefix[0] . generaterandomnumber(6); elseif(key($string_preprefix) == 'b'):     echo $string_preprefix[0] . $string_prefix[0] . generaterandomnumber(7); endif; 

in addition, agree @iandrake's comment - shuffle() doesn't want. you'll want perform randomization of array.


Comments