[Solved] From numeric Phone # to alphatized... How???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

[Solved] From numeric Phone # to alphatized... How???

Post by DaveTheAve »

Alright, I won't post something asking for help if I didn't try to attack the problem myself. Below you will see my (poor) excuse for attempting to transform a phone number (123-5405) into many strings that can be dialed into a phone. Basically, want I wanted was a list containg numerous (most likely 3k) possible stings that can be dialed into a phone that will call the same number.

Any ideas how to tackle this beast?

Code: Select all

<?php

/* Don't laugh but here is my thinking w/o coffee at 10PM at night. */

$numberArray = array(
	0 => array('0'),
	1 => array('1'),
	2 => array('a','b','c'),
	3 => array('d','e','f'),
	4 => array('g','h','i'),
	5 => array('j','k','l'),
	6 => array('m','n','o'),
	7 => array('p','q','r','s'),
	8 => array('t','u','v'),
	9 => array('w','x','y','z')
	);
	
$number = array(1,2,3,5,4,0,5);

//Go through whole number
for($i=0,$k=count($number);$i<$k;$i++)
{
	//Pull out letters
	for($i=0,$k=count($number[$i]);$i<$k;$i++)
	{
                /* P.S. - Yes I know I use $i and $k again.... i was only thinking not actually running this code. */
		$combo = $numberArray[(int)$number[$i]]
	}
}
Last edited by DaveTheAve on Thu Mar 22, 2007 3:32 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I think the most important part would be finding meaningful combinations of those letters... have you considered a different interface? How about 7 drop-down boxes in a row?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

@DaveTheAve

it seems to me the idea of "cartesian product" (http://en.wikipedia.org/wiki/Cartesian_product) would be helpful here. From "$numberArray" select subarrays that match the number (e.g. for 123-32 this would be arrays 1,2,3,3,2) into a new array, then apply a Product function to that new array. At the end you'll get all possible letter combinations that correspond to the phone number.

A quick'n'dirty cartesian product implementation in php:

Code: Select all

function product($a) {
	if(count($a) == 0)
		return array(array());
	foreach(product(array_slice($a, 1)) as $p)
		foreach($a[0] as $e)
			$r[] = array_merge(array($e), $p);
	return $r;
}
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

Kieran Huggins wrote:I think the most important part would be finding meaningful combinations of those letters... have you considered a different interface? How about 7 drop-down boxes in a row?
Thank you, but i'm not aiming for any-type of GUI right now.

@stereofrog:

Thank you for mentioning Cartesian Products, i'm reasearching just what they are now but i'm getting lost on how this would help; give me some time and I might see where your comming from.

Edit: Yeah, I don't get where your comming from even with the help of your php snippet; I'm sorry.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

The complete code is as follows (I'm using product() from my post, and $number and $numberArray from yours):

Code: Select all

// select only digits we need

foreach($number as $digit)
	$digits[] = $numberArray[$digit];


// build the Product of selected digits

$prod = product($digits);


// output all combinations found 

foreach($prod as $combination)
	echo implode('', $combination), "<br>\n";
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

Wow... soo small and complex but it works.... it really works!!! Thank you, I'll study this very hard!
Post Reply