Page 1 of 1

Printing arrays

Posted: Sun Apr 28, 2002 9:53 pm
by hob_goblin
in my script i have something like:

$num = 2;
$some_nums = ($_1, $_2, $_3);
$final_num = array_rand($some_nums, $num);

how would i print every single value of "$final_num"?

Posted: Sun Apr 28, 2002 10:03 pm
by dusty

Code: Select all

foreach($array as $var) {
  echo "$var";
}

Posted: Sun Apr 28, 2002 10:05 pm
by jason
Or, if you just need a quick and dirty display of the array:

var_dump() or print_r()

Posted: Sun Apr 28, 2002 10:34 pm
by hob_goblin

Code: Select all

<?
if($whattodo == "generate")&#123;
echo "doing your work...";
function rsl()&#123;
$randomsnum = mt_rand(97, 122);
$schar = chr($randomsnum);
return($schar);
&#125;

function rbl()&#123;
$randomlnum = mt_rand(65, 90);
$bchar = chr($randomlnum);
return($nchar);
&#125;

function rsn()&#123;
$randomnnum = mt_rand(48, 57);
$nchar = chr($randomnnum);
return($nchar);
&#125;

$_1 = rsl();
$_2 = rbl();
$_3 = rsn();
$_4 = rsl();
$_5 = rbl();
$_6 = rsn();
$_7 = rsl();
$_8 = rbl();
$_9 = rsn();
$_10 = rsl();
$_11 = rbl();
$_12 = rsn();
$_13 = rsl();
$_14 = rbl();
$_15 = rsn();
$_16 = rsl();

$final_set = array($_2, $_3, $_4, $_5, $_6, $_7, $_8, $_9, $_10, $_11, $_12, $_13, $_14, $_15, $_16);
$num_final = $num - 1;
$final_num = array_rand($final_set, $num_final);

foreach($final_num as $var) &#123; 
  echo "$var"; 
&#125;






&#125; else &#123;
echo "Type in chars:";
echo "<form method=POST action="$PHP_SELF"><input type=hidden name=whattodo value=generate><input type=text name=num><br>";
echo "<input type=submit value=submit></form>";
&#125;
?>

basically what i want to do is generate a password... that the user inputs how many characters.. and it generates it, random letters, and numbers. Mixed case.

After that I will use it with another script im writing..

Posted: Sun Apr 28, 2002 10:52 pm
by dusty

Code: Select all

<?
if($submit) &#123;
  $pass = "";
  $az09 = "abcdefghijklmnopqrstuvwxyz";
  $az09 .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $az09 .= "0123456789";

  while(strlen($pass) < $num) &#123;
    mt_srand((double) microtime() * 1000000);
    $pass .= substr($az09, mt_rand(0, strlen($az09) - 1), 1);
  &#125;

  echo "$pass";
&#125; else &#123;
  echo "Type in chars:";
  echo "<form method="POST" action="$PHP_SELF">";
  echo "<input type="text" name="num"><br>";
  echo "<input type="submit" name="submit" value="submit">";
  echo "</form>";
&#125;
?>
might be another solution.

Posted: Sun Apr 28, 2002 11:25 pm
by hob_goblin
wow, thanks ALOT, that worked instantly...and did everything I wanted. except with about 1/3 of the coding i was going to write out.

Posted: Mon Apr 29, 2002 2:44 pm
by hob_goblin
http://www.ice-on-fire.net/dpinclu.php incase you want to see the finished product. :D

Posted: Mon Apr 29, 2002 2:59 pm
by dusty
nice ;]

Posted: Tue Jun 17, 2003 5:15 pm
by diehuman
That is the coolest 404 page I have ever seen, haha.