Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional Arrays help

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
I have been trying to learn more about Multidimensional Arrays. How do i get lettercount in this array only based off the letter not the index number...


here is my array output:
Code:
$alpha_count => Array (22)
(
|    ['0'] => Array (2)
|    (
|    |    ['letter'] = String(1) "a"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['1'] => Array (2)
|    (
|    |    ['letter'] = String(1) "b"
|    |    ['lettercount'] = String(1) "3"
|    )
|    ['2'] => Array (2)
|    (
|    |    ['letter'] = String(1) "c"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['3'] => Array (2)
|    (
|    |    ['letter'] = String(1) "d"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['4'] => Array (2)
|    (
|    |    ['letter'] = String(1) "e"
|    |    ['lettercount'] = String(1) "2"
|    )
|    ['5'] => Array (2)
|    (
|    |    ['letter'] = String(1) "f"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['6'] => Array (2)
|    (
|    |    ['letter'] = String(1) "g"
|    |    ['lettercount'] = String(1) "2"
|    )
|    ['7'] => Array (2)
|    (
|    |    ['letter'] = String(1) "h"
|    |    ['lettercount'] = String(1) "7"
|    )
|    ['8'] => Array (2)
|    (
|    |    ['letter'] = String(1) "i"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['9'] => Array (2)
|    (
|    |    ['letter'] = String(1) "j"
|    |    ['lettercount'] = String(1) "2"
|    )
|    ['10'] => Array (2)
|    (
|    |    ['letter'] = String(1) "k"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['11'] => Array (2)
|    (
|    |    ['letter'] = String(1) "l"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['12'] => Array (2)
|    (
|    |    ['letter'] = String(1) "m"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['13'] => Array (2)
|    (
|    |    ['letter'] = String(1) "n"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['14'] => Array (2)
|    (
|    |    ['letter'] = String(1) "o"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['15'] => Array (2)
|    (
|    |    ['letter'] = String(1) "p"
|    |    ['lettercount'] = String(1) "2"
|    )
|    ['16'] => Array (2)
|    (
|    |    ['letter'] = String(1) "r"
|    |    ['lettercount'] = String(1) "2"
|    )
|    ['17'] => Array (2)
|    (
|    |    ['letter'] = String(1) "s"
|    |    ['lettercount'] = String(3) "343"
|    )
|    ['18'] => Array (2)
|    (
|    |    ['letter'] = String(1) "t"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['19'] => Array (2)
|    (
|    |    ['letter'] = String(1) "v"
|    |    ['lettercount'] = String(1) "2"
|    )
|    ['20'] => Array (2)
|    (
|    |    ['letter'] = String(1) "w"
|    |    ['lettercount'] = String(1) "1"
|    )
|    ['21'] => Array (2)
|    (
|    |    ['letter'] = String(1) "z"
|    |    ['lettercount'] = String(1) "1"
|    )
)

Jason

[red]Army[/red] : [white]Combat Engineer[/white] : [blue]21B[/blue]

 
that's not quite the way to create your array, to be honest.

better would be this.
You could then address each frequency by referring to
$output['letter'];

Code:
<?php
$string = "I am a string";

$output = getFrequency($string);
print_r($output);

function getFrequency($string){
	//easier this way.  if case sensitivity required, delete this line
	$string = strtolower($string);
	//make dummy array of zero freqs for each letter
	for($l=97;$l<=122; $l++){
		$dummy[chr($l)] = 0;
	}
	
	//check the actual string
	foreach (count_chars($string, 1) as $letter=>$freq){
		//capture only alpha
		if ($letter >= 97 && $letter <=122){
			$output[chr($letter)] = $freq;
		}
 	}
	 //sort the array
	 ksort($output, SORT_STRING);
	 //merge the array with the dummy to keep the zero vals
	 $output = array_merge($dummy, $output);
	 return $output;
}
?>
 
From what you already have, you'd need to loop through that array with a FOR, FOREACH, or WHILE statement. Jpadie's suggestion would return a hash that would be better to use...something like...

['a'] => 1
['b'] => 3
...

From what you already have, you could...

Code:
$newarray = array();
foreach ($alpha_count as $row){
   $letter = strtolower($row['letter']);
   $count = $row['lettercount'];

   $newarray[$letter] = $count;
}

But I would try to skip this step and make the output array easier to use from the start.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top