Jump to content

UNDEFINED OFFSET FOR SORTING WORDS


booxvid

Recommended Posts

This program is to sort words into qwerty order.

 

<?php
//	include 'qwerty.php';

$words = array("one,two,free");
$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();
				$text = "one two free";

				$words = explode(" ",$text);

				for($i=0;$i<count($words);$i++){

					$next_word = $words[$i];
					$fletter_nextword = get_first_letter($next_word);
					$key_nextword =get_key($fletter_nextword,$order);

					$key[$i] = $key_nextword;

				}

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

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

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

function get_first_letter($this_word){

	$temp = explode(' ',$this_word);
	return $temp[0];
	}

function get_key($the_word,$order){
	$key="";
	$i=0;

	do{
		if($the_word==$order[$i]){
			$flag = "TRUE";
			$key = $i;
		}
		else{
			$flag = "FALSE";
		}

		$i++;
	}while($flag=="FALSE");


	return $key;
}

?>



 

It says :

Notice: Undefined offset: 26 in C:\xampp\htdocs\OOP_SYSTEM\qwerty sort\sort_qwerty.php on line 48

 

Link to comment
Share on other sites

Oh sorry, I've already updated my codes.

 

<?php
//	include 'qwerty.php';

$words = array("one,two,free");
$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();
				$text = "one two free";

				$words = explode(" ",$text);

//					for($i=0;$i<count($words);$i++){
//						
					$next_word = $words[0];
					echo $next_word;
					$temp = array();

					echo $temp[0];
					//$fletter_nextword = get_first_letter($next_word);
//						$key_nextword =get_key($fletter_nextword,$order);
//							
//						$key[0] = $key_nextword;
//						echo $key[0];
//			
//					}

//					$words_key = array_combine($key, $words);
//					ksort($words_key);
//					
//					foreach ($words_key as $key => $value){
//						
//						echo $value.'</br>';
//					}

function get_first_letter($this_word){

	$temp = explode(' ',$this_word);
	return $temp[0];
	}

function get_key($the_word,$order){
	$key="";

	for($i=0;$i<count($order);$i++){
		if($the_word==$order[$i]){		
				$key = $i;
				break;
			}
	}

	return $key;
}

?>


 

my problem right now is how to get the first letter of a string.

Link to comment
Share on other sites

Thanks! I'm almost done with this sorting work.

function get_key($the_letter,$order){
	$key="";

	for($i=0;$i<count($order);$i++){
		if($the_letter==$order[$i]){		
				$key = $i;
				break;
			}
	}

	return $key;
}

 

got one last question:

 

is my function correct? by when I loop in with the array of qwerty, together the first word that I've got

is this the right way in stopping a for loop?

 

Link to comment
Share on other sites

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.