User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 401,456 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,983 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 847 | Replies: 3
Reply
Join Date: Sep 2005
Posts: 139
Reputation: aarya is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
aarya's Avatar
aarya aarya is offline Offline
Junior Poster

php arrays

  #1  
May 4th, 2006
this is the code below it output the text randamly. i am getting the output . but my problem is i have used print_r to get output
so i got result as


<Array ( [0] => gello [1] => bello [2] => nello [3] => jello [4] => uello [5] => yello [6] => hwllo [7] => hsllo [8] => hdllo [9] => hrllo [10] => h4llo [11] => h3llo [12] => heklo [13] => heplo [14] => heolo [15] => helko [16] => helpo [17] => heloo [18] => helli [19] => hellk [20] => helll [21] => hellp [22] => hell0 [23] => hell9 ) 

but want
gello
bello
nello like this

any body help how to conver an array to normal words without array[0] ext

<?
$word = "Hello";
$typos = array();
$typos = cTypoGenerator::getWrongKey( $word );



   print_r ($typos);





class cTypoGenerator
{

// array of keys near character on a QWERTY keyboard
// only valid characters in a domain name
	static $keyboard = array(
// top row
		'1' => array( '2', 'q' ),
		'2' => array( '1', 'q', 'w', '3' ),
		'3' => array( '2', 'w', 'e', '4' ),
		'4' => array( '3', 'e', 'r', '5' ),
		'5' => array( '4', 'r', 't', '6' ),
		'6' => array( '5', 't', 'y', '7' ),
		'7' => array( '6', 'y', 'u', '8' ),
		'8' => array( '7', 'u', 'i', '9' ),
		'9' => array( '8', 'i', 'o', '0' ),
		'0' => array( '9', 'o', 'p', '-' ),
		'-' => array( '0', 'p' ),
// 2nd from top
		'q' => array( '1', '2', 'w', 'a' ),
		'w' => array( 'q', 'a', 's', 'e', '3', '2' ),
		'e' => array( 'w', 's', 'd', 'r', '4', '3' ),
		'r' => array( 'e', 'd', 'f', 't', '5', '4' ),
		't' => array( 'r', 'f', 'g', 'y', '6', '5' ),	
		'y' => array( 't', 'g', 'h', 'u', '7', '6' ),
		'u' => array( 'y', 'h', 'j', 'i', '8', '7' ),
		'i' => array( 'u', 'j', 'k', 'o', '9', '8' ),
		'o' => array( 'i', 'k', 'l', 'p', '0', '9' ),
		'p' => array( 'o', 'l', '-', '0' ),
// home row
		'a' => array( 'z', 's' , 'w', 'q' ),
		's' => array( 'a', 'z', 'x', 'd', 'e', 'w' ),
		'd' => array( 's', 'x', 'c', 'f', 'r', 'e' ),
		'f' => array( 'd', 'c', 'v', 'g', 't', 'r' ),
		'g' => array( 'f', 'v', 'b', 'h', 'y', 't' ),
		'h' => array( 'g', 'b', 'n', 'j', 'u', 'y' ),
		'j' => array( 'h', 'n', 'm', 'k', 'i', 'u' ),
		'k' => array( 'j', 'm', 'l', 'o', 'i' ),
		'l' => array( 'k', 'p', 'o' ),
// bottom row
		'z' => array( 'x', 's', 'a' ),
		'x' => array( 'z', 'c', 'd', 's' ),
		'c' => array( 'x', 'v', 'f', 'd' ),
		'v' => array( 'c', 'b', 'g', 'f' ),
		'b' => array( 'v', 'n', 'h', 'g' ),
		'n' => array( 'b', 'm', 'j', 'h' ),
		'm' => array( 'n', 'k', 'j' )
	);




	function getWrongKey( $word )
	{
		$word = strtolower( $word );
		$typos = array();
		$length = strlen( $word );
// check each character
		for( $i = 0; $i < $length; $i++ )
		{
// if character has replacements then create all replacements
			if( cTypoGenerator::$keyboard[$word{$i}] )
			{
// temp word for manipulating
				$tempWord = $word;
				foreach( cTypoGenerator::$keyboard[$word{$i}] as $char )
				{
					$tempWord{$i} = $char;			
					array_push( $typos, $tempWord );
				}
			}
		}

		return $typos;
	}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: php arrays

  #2  
May 4th, 2006
You cannot access elements of an array without using the square brackets, in general. That's what an array IS, a collection of elements, and you access them via the proper key, delimited by square brackets.

You can implode an array into a single delimited array, but then you'd have to do a split to get your "normal words"... but they'd be an array of substrings, so you're back to using square brackets.

I guess I don't understand the question... what would your "normal words" look like, in terms of variables?
Reply With Quote  
Join Date: Aug 2005
Posts: 76
Reputation: leelee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
leelee leelee is offline Offline
Junior Poster in Training

Re: php arrays

  #3  
May 5th, 2006
The function that outputs the array in the example you've given is:
print_r ($typos);
There are a number of ways to output in the manner in which you require, and most will use a loop of some kind. As a starter for 10, try outputting with a foreach statement:
foreach($typos as $key) { echo "$key\n"; }
Reply With Quote  
Join Date: Sep 2005
Posts: 139
Reputation: aarya is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
aarya's Avatar
aarya aarya is offline Offline
Junior Poster

Re: php arrays

  #4  
May 5th, 2006
Originally Posted by leelee
The function that outputs the array in the example you've given is:
print_r ($typos);
There are a number of ways to output in the manner in which you require, and most will use a loop of some kind. As a starter for 10, try outputting with a foreach statement:
foreach($typos as $key) { echo "$key\n"; }



thank you so much it worked.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC