Jump to content

Array is overwritten by the keys


booxvid

Recommended Posts

this is my main code

<?php
include 'qwerty.php';

$words = array();
$order = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m');
$key = array();

//for added val keys
$abc = array ("a","b","c","d");

			$text = "class top clone house apple blue";

			$words = explode(" ",$text);
			$flag ="";
				for($i=0,$j=count($words);$i<count($words);$i++,$j--){
					$add_value = 0;
					$next_word = $words[$i];

					$fletter_nextword = Qwerty::get_first_letter($next_word);
					echo $fletter_nextword."<br/>";
						if(!in_array($fletter_nextword,$order)){
							echo "there's a number in a string";
							$flag="FALSE";
							break;
						}
						else{
							$key_nextword = Qwerty::get_key($fletter_nextword,$order);
							echo $key_nextword.'<br/>';
							if(in_array($key_nextword,$key)){
								$key[$i] = $key_nextword.'b';
								$flag = "TRUE";
							}
							else{

								$key[$i] = $key_nextword.'a';
								$flag = "TRUE";	

							}

						}
				}

			if($flag!="FALSE"){

				$words_key = array_combine($key,$words);
				ksort($words_key);

				foreach ($words_key as $key => $value){

					echo $value.' '.$key.'</br>';
				}
		}

?>



 

the problem is, when I'm trying to sort these words by the assigned array, what happen is

EXAMPLE:

close and class does have same first letter which means, I assigned it already that if the word starts with letter c

the key should be is 21

 

this is according to the qwerty order what happen with the words close and class...

close will not be included in the printed words in a sort out qwerty order /:

 

how to put up extra added key with the same key in it?

Link to comment
Share on other sites

I meant to reply to your other post last night. Is this homework, or an actual application? This can be accomplished with usort and a simple callback function using a for loop and array_search -- 10 or 15 lines of code (I think).

 

When I get home (and done baby-sitting), I'll take another look at it and provide a more detailed answer.

 

Link to comment
Share on other sites

The code below will sort any array of words in "qwerty" order -- I don't know why anyone would want to, but here it is.

 

<?php

/* $CONST_order - we need to access this array in a callback function as a global. So,
	I gave it a special name to help prevent modifications to the array elsewhere in
	the application. */
$CONST_order = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m');

// Some sample words to be sorted
$text = "class top clone house apple blue";
#	$text = "classy appleoyou top class to-me home house apple4you";

// Turn the text into an array of words to be sorted
$words = explode(' ', $text);
print_r($words);

// Sort the array based on the $CONST_order sequence
usort($words, 'CB_qwertySort');

// Print the results
print_r($words);

exit;


/* CB_qwertySort - This function accepts two words. It returns an integer indicating
		the "order" of the two words: -1 if $word1 is LESS THAN $word2, 0 if $word1
		is EQUAL TO $word2, or +1 if $word1 is GREATER THAN $word2.

	Note: This function ignores the fact that there may be characters in the words that
		are not in the sort order. The array_search function will return FALSE, which
		we treat as zero, so they will sort equivalent to "q". We could easily test
		for this and sort these "special characters" as less than or greater than any
		other letter in the sort order. */
function CB_qwertySort($word1, $word2) {
	/* I know, globals are a no-no. But I know no other way to get the array into this
		function. We have taken precautions. */
	global $CONST_order;

	$len1 = strlen($word1);
	$len2 = strlen($word2);
	$lenMin = min($len1, $len2);

	/* Check each character position using the length of the shorter of the words.
		If one word is shorter and all characters up to that length are equal, then
		the shorter word is LESS THAN the longer one. */
	for ($i = 0; $i < $lenMin; ++$i) {
		$pos1 = array_search(strtolower($word1{$i}), $CONST_order);
		$pos2 = array_search(strtolower($word2{$i}), $CONST_order);
		if ($pos1 < $pos2) return -1;
		elseif ($pos1 > $pos2) return +1;	// "+" just to be clear
	}
	/* If we get here, the words are the same, or one is shorter and they are the
		same for the length of the shorter word. The shorter word is considered LESS THAN
		the longer word */
	if ($len1 < $len2) return -1;
	elseif($len1 > $len2) return +1;

	// They must be exactly the same
	return 0;
}

It's 60 lines, but if you don't count the opening php tag and the blank lines and comments, and you take out the print_r calls and the exit (which is not needed) and ignore the two setup lines at the beginning, it's 17 lines of code (2 lines over my guess) -- Damn I'm good! (I have to say that every now and then, because no one around here ever does).

 

I use the curly-brace operator (that is an operator, right?) to index the words. Note that: $word1{0} === $word1[0]. That is, you can use the array square-brackets to index a string (as if it is an array of characters). I use the curly-brace, because it adds a level of documentation to the code. It makes it clear that $word1 is a string not some array that was passed in.

 

I did use global but the only way I see to avoid it is to make it a class. If anyone has a suggestion, I'd like to hear it.

 

Since the order array is all lower-case, I did the compare in lower-case, so the sort is case-insensitive. If you add the 26 Upper Case letters to the sort order array, and remove the strtolower() function calls, it will be case-sensitive.

 

Other "issues" are documented in the code.

 

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.