traversing multidimensional arrays

i have been at this segment of code for the last 5 days. since i have not made any progress i decided to come here…
i am trying to replace $name with the values in the $search array. though for some reason i am only getting 184 “a” and 5 variations of “a”. you can see what i mean here: http://tibtibs.com/godaddybegay/pass_gen_II/server_info.php
this is my first multidimensional array. so i am sure i am doing wrong. some insight would be greatly appreciated.

[php]
$search = array( array(“a”=>array(‘4’,’@’,’^’,‘aye’,‘ci’)),
array(“b”=>array(‘8’,’|3’,‘6’,‘13’,’|3’,‘ß’,’]3’)),
array(“c”=>array(’(’,’<’,‘¢’,’{’,‘©’,‘sea’,‘see’)),
array(“d”=>array(’|)’,’[)’,’])’,‘I>’,’|>’,‘0’,‘ð’,‘cl’)),
array(“e”=>array(‘3’,‘£’,’&’,‘€’,’[-’)),
array(“f”=>array(’|=’,’]=’,’}’,‘ph’,’(=’)),
array(“g”=>array(‘6’,‘9’,’&’,’(+’,‘C-’,‘gee’,‘jee’,‘cj’)),
array(“h”=>array(’|-|’,’]-[’,’[-]’,’)-(’,’(-)’,’ :-:’,’}{’,’}-{’,’#’,‘aych’)),
array(“i”=>array(’!’,‘1’,’|’,‘eye’,‘3y3’,‘ai’,’¡’)),
array(“j”=>array(’
|’,’/’,’]’,’¿’,’)’)),
array(“k”=>array(‘X’,’|<’,’|X’,’|{’)),
array(“l”=>array(‘1’,‘7’,’|’,‘£’,’|’,’|’,‘lJ’,‘¬’)),
array(“m”=>array(’|/|’,‘em’,’|v|’,’[V]’,’^^’,‘nn’,’//\//\’,’(V)’,’(/)’,’/|/|’,’.\’,’|^^|’)),
array(“n”=>array(’||’,’//’,’//\//’,’[]’,’<>’,’{}’,’//’,’[][]’,’][’,’~’)),
array(“o”=>array(‘0’,’()’,‘oh’,’[]’,‘¤’,‘O’)),
array(“p”=>array(’|*’,’|o’,’|º’,’|>’,’|"’,‘9’,’[]D’,’|7’,‘q’,‘þ’,’¶’,’|D’)),
array(“q”=>array(‘0_’,‘0,’,’(,)’,’<|’,‘cue’,‘9’,’¶’)),
array(“r”=>array(’|2’,‘2’,’/2’,‘I2’,’|^’,’|~’,‘lz’,‘®’,’|2’,’[z’,’|','l2','.-')), array("s"=>array('5','$','z','§','es')), array("t"=>array('7','+','-|-','1','†')), array("u"=>array('|_|','(_)','Y3W','M','µ','[_]','\_/','/_/')), array("v"=>array('\/','v','\\//')), array("w"=>array('\/\/','vv','\^/','(n)','\X/','\|/','\_|_/','\\//\\//','\_:_/',']I[','UU')), array("x"=>array('%','><','}{','ecks','×','*',')(','ex')), array("y"=>array('/’,’`(’,’-/’,‘f’,‘¥’)),
array(“z”=>array(‘2’,’=’,’~/’,’ %’,'7’)),
);

$name = “Sabrina”;
$name = str_split(strtolower($name));
foreach ($search as $key => $value){
foreach ($value as $subkey => $subvalue){
foreach ($subvalue as $subsubvalue){
foreach ($name as $letter){
$replaced = str_replace($subkey,$subsubvalue,$letter);
}
echo “$replaced”;
}
}
}
[/php]

Hi there,

Nice idea - try this:
[php]
$name = “SmokeyPHP”;
$name = str_split(strtolower($name));
$newname = “”;
foreach($name as $letter)
{
foreach($search as $group)
{
foreach($group as $ltr => $options)
{
$choice = rand(0,count($options)-1);
if($ltr == $letter) $newname .= $options[$choice];
}
}
}
echo $newname;
[/php]

awesome, thank you so very much. :-*

Not a problem, glad to have helped.

okay, one more time… for now. i hope.
each part seems to work on its own. the while is looping though a dictionairy file, and the first foreach loop when echo’d diplays just fine. however when i try to pass the words onto the search array ( $i1337intel now) i get Invalid argument supplied for foreach() on line 39. i commented on line 39.

[php]$file_handle = fopen(“dictionary.txt”, “rb”);
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(’\n’, $line_of_text);
$name = “”;
$newname = “”;
foreach($parts as $name){
foreach($name as $letter){ //<–line 39
foreach($i1337intel as $group){
foreach($group as $ltr => $options){
$choice = rand(0,count($options)-1);
if($ltr == $letter) $newname .= $options[$choice];
}
}
}return $newname;
}
}
fclose($file_handle);[/php]

I think your problem is that fgets() returns the single line, therefore you do not need to explode by “\n”.

Try removing the outer foreach loop ($parts as $name) and replace the “$parts = explode…” line with:

$name = preg_split("//",$line_of_text,-1,PREG_SPLIT_NO_EMPTY);

still recieving invalid foreach. [php]foreach($name as $letter){ [/php]

not sure if it makes a diffirence, the dictionairy file is already one word per line.

No that doesn’t make a difference, however I have just noticed that $name is resetting to “” after the preg_split. So you may want to delete the line that does that - see if that helps it at all.

now i am getting a blank page, i changed the code to look as such:
[php]
$newname = “”;
$file_handle = fopen(“dictionary.txt”, “rb”);
while (!feof($file_handle) )
{
$line_of_text = fgets($file_handle);
$name = preg_split("//",$line_of_text,-1,PREG_SPLIT_NO_EMPTY);
foreach($name as $letter)
{
foreach($i1337intel as $group)
{
foreach($group as $ltr => $options)
{
$choice = rand(0,count($options)-1);
if($ltr == $letter) $newname .= $options[$choice];
}
}
}return $newname;
}fclose($file_handle);

[/php]
i tried changing return $newname to echo $newname, but that only spammed the page with never ending variations of “a”. which is the fist word in the dictionary.txt file.

okay, apologies for the delay but I’ve tested the following with my own dictionary.txt with one name on each line, the first name starting with “a”. It worked fine for me, hopefully this works fine for you:

		$file_handle = fopen("dictionary.txt", "rb");
		while (!feof($file_handle) )
		{
			$newname = "";
			$line_of_text = fgets($file_handle);
			$name = preg_split("//",$line_of_text,-1,PREG_SPLIT_NO_EMPTY);
			foreach($name as $letter)
			{
				foreach($i1337intel as $group)
				{
					foreach($group as $ltr => $options)
					{
						$choice = rand(0,count($options)-1);
						if($ltr == $letter) $newname .= $options[$choice];
					}
				}
			}
			echo $newname;
			echo "<br />";
		}
		fclose($file_handle);

awesome! thank you again so much. the out put for my dictionary file is here :http://tibtibs.com/godaddybegay/pass_gen_II/dictionary.php
and the password generator is here:http://tibtibs.com/godaddybegay/pass_gen_II/password_gen.php
any recommended books i should read about php?

http://tibtibs.com/godaddybegay/pass_gen_II/test_file.php

i noticed that each letter has been giving a number from 1 - 26, how would i match an array to those numbers?

i might as well ask all the questions that come to mind right. anyway i got that list from var_dump().

Sorry but in what way? What are you trying to achieve?

take a numeric string, split it into one or two digit numbers that are between 1 and 26, and match the numbers to the corresponding letter. another part of traversing i am not getting.

OK, well if it makes it clearer what it returns, the following returns the same as the $group variable in the foreach loops (gives the array where the letter is the first element whose value is the array of choices).

[php]$num = 13-1;
$group = $i1337intel[$num];
[/php]

I have put a -1 in the setting of number as you would probably be pulling in a number from another variable instead of a hard-coded 13 and because arrays start from 0 rather than 1, you need to minus one from the place in the alphabet in order to get the place in the array.

here is what i am understanding as to how this array works…
array(26) { [0]=> array(1) { [“a”]=> array(5) { [0]=> string(1) “4”

there are 26 sub arrays in the one array, of those 26 options [0] is to the first array which is [“a”]. [“a”] contains 5 expressions, of which the first labeled [0] is a string with 1 character that is “4”.
we use foreach() to cycle through each segment of the array as its far simpler then using for() or manually placing and reseting the pointers.
what i am not understanding is if a random number were given, say… “1812141042415715”… how would i match that to “smokeyphp” in $i1337intel ?

i tried echo $i1337intel[13]; and ended up with “Array”.

forgot to wrap the output above. so here it is… sorry for the double post.

array(26) { [0]=> array(1) { ["a"]=> array(5) { [0]=> string(1) "4" 

Hi there,

One character from the number string at a time works with this:
[php]$number = 123456;
$number = preg_split("//",$number,-1,PREG_SPLIT_NO_EMPTY);
foreach($number as $num)
{
foreach($i1337intel[$num-1] as $letter => $options)
{
$rand = rand(0,count($options)-1);
echo $options[$rand];
}
}
[/php]

Choosing groups of numbers less than or equal to 26 and assigning them their letter (haven’t tested thoroughly):
[php]$number = 326412;
$numbera = preg_split("//",$number,-1,PREG_SPLIT_NO_EMPTY);
$i=0;
while($i < strlen($number))
{
$num = $numbera[$i];
if(isset($numbera[$i+1]) && ($num*10 + ($numbera[$i+1])) <= 26)
{
$i++;
$num .= $numbera[$i];
}
foreach($i1337intel[$num-1] as $letter => $options)
{
$rand = rand(0,count($options)-1);
echo $options[$rand];
echo “
”;
}
$i++;
}
[/php]

that did it. seems to throw out

Warning: Invalid argument supplied for foreach()
at random.
[php]
foreach($i1337intel[$num-1] as $letter => $options)
[/php]

http://tibtibs.com/godaddybegay/pass_gen_II/test_file.php
and so you can see where the numbers are coming from:
[php]
$randInt = rand(111111111,999999999);
$IP = ereg_replace("[^0-9]", “”, $_SERVER[‘REMOTE_ADDR’]);
$time = time();
$number =$IP;
$number .=$time;
$number .=$randInt;
$numbera = preg_split("//",$number,-1,PREG_SPLIT_NO_EMPTY);
$i=0;
while($i < strlen($number))
{
$num = $numbera[$i];
if(isset($numbera[$i+1]) && ($num*10 + ($numbera[$i+1])) <= 26)
{
$i++;
$num .= $numbera[$i];
}
foreach($i1337intel[$num-1] as $letter => $options)
{
$rand = rand(0,count($options)-1);
echo $options[$rand];
echo " ";
}
$i++;
}
[/php]

cool, to fix the foreach issue try just putting

[php]if(isset($i1337intel[$num-1]))
[/php]

around it

Sponsor our Newsletter | Privacy Policy | Terms of Service