Jump to content

Some Php Sorting And Array Issues


jblack

Recommended Posts

Well I know it's not pretty, but the basic problem here is that after I get the words sorted in $list in alphabetical order I am unable to split the resulting strings into individual characters and then alphabetize them. I'm looking to get something that looks like this: $str[0] { [0] => a [1] => d ... [3] =>s}$str[1] { [0] => a [1] => e ... [6] =>u} and so on. I'm just doing this for no specific reason other than to give myself something to do. Any help would be greatly appreciated. My script is below. (wee bit messy)

<?php$list = "asdfmaybescriptmightelwoodatherus";$i = 0;$list = explode("\n","$list");$number = count($list);sort($list);while ($i <= $number) {echo "$list[$i]<p />";$i++;}$i = 0;while ($i <= $number) {$str[$i] = preg_split('//', $list[$i]);$i++;}$i = 0;while($i <= $number) {$a = $i;$count[$i] = count($str);$i++;}$i = 0;while ($i <= $count[$i]) {echo $count[$i];$a = 1 + $i;sort($str[$i]);echo "$str[$i][$a]<p />";$i++;}echo "$str[3][3]";?>

Link to comment
Share on other sites

PHP has an in-built sorting function.You can also sort by your own critera. The usort() function takes a callback function where the two parameters refer to two arbitrary elements of the array. If the first element should go before the second then return 1, if it should be the other way around then return -1. If the two elements have equal prevalence, return 0.

Link to comment
Share on other sites

I think $str should be $str[$i] in this line:

$count[$i] = count($str);

The 4 <= signs should be < Also if your running it on windows, the words will most likely be separated by \r\n, so you may need to change \n to \r\n in explode().

Link to comment
Share on other sites

Well I've already got the list sorted and that works, I just can't seem to get str_split or preg_split to work, or they work and I can't get the letters to sort. I'm not entirely sure if preg_split is working, but str_split wasn't working for me at all.

Link to comment
Share on other sites

str_split is not a complex function, it sounds like you either weren't using the function correctly or you weren't using the return value correctly. Show your code where you use str_split if you want help with that. Otherwise, check the manual. There's no reason to use a regular expression if you're just trying to split up a string. In fact, if you want to access each character in a string you don't even need to split it, just use array syntax to access the characters.

$str = 'some string';for ($i = 0; $i < strlen($str); $i++)  echo $str[$i] . '<br>';

http://www.php.net/manual/en/function.str-split.php

Link to comment
Share on other sites

@justsomeguy I am aware that you can access a character in a string like that, but thank you anyway. I use the regular expression because I can't seem to get str_split work. Can you alphabetize a string without splitting up the characters though?

Link to comment
Share on other sites

What do you mean by "alphabetize a string"? If you're talking about sorting, there are several built-in functions that will sort arrays in several different ways. This is how you use str_split to get an array of characters from a string:$chars = str_split($str, 1);

Link to comment
Share on other sites

well, lets use "atherus" and "asdf" as an example. after I split $list into it's separate parts I sorted that into alphabetical order, therefore $list[0] = asdf and $list[1] = atherus. Then I want to split those elements of $list into characters and alphabetize them so I get $str[1][0] = a and $str[1][1] = e and so on for the other parts of $list. Or something to that effect so that I can compare a larger list to a few words that are scrambled. This is just the beginning of the prototype to get things working.

Link to comment
Share on other sites

What I'm wanting to do, though, is after I get the characters sorted I want to be able to echo them so I would getadfsaehrstuedloow...And if I get an anagram of atherus such as ahreust I can sort it into aehrstu and compare it to the presorted list and reverse it to find atherus. Honestly I'm making an anagram solver with a set list of words to compare to. I got it to sort the characters into a multidimensional array alphabetically now, but the issue is that $str[0][0] and $str[0][1] both are '', then $str[0][2] = a. And the rest is in alphabetical order. The issue is that on the second dimension the strings start at [2] rather than [0]

Link to comment
Share on other sites

Well, I tried trimming each word in the $list array but when I go to split up the characters I still get [0]'s and [1]'s as empty values. I'm also having troubles getting the alphabetical strings back into a 1 dimensional array, where as $alpha[0] = adfs and $alpha[1] = aehrstu.

Link to comment
Share on other sites

You can use implode to turn the array back into a string: http://www.php.net/manual/en/function.implode.php Post your code if you want help with the other issue, it still sounds like there's whitespace around the string. str_split wouldn't create empty values if there aren't any characters there. You can use var_dump on the array to check the structure, I bet you'll find that the entries that you think are blank have a length of 1 instead of a length of 0.

Link to comment
Share on other sites

Alright here's my updated script (some words changed for unrelated reasons). It's still kind of messy. I also echo quit a bit to make sure I'm getting the general results that I want. output at www.twdesignjb.com/test.php

<?php$list = "helpthisscriptmightelwoodatherus";$i = 0;$list = explode("\n","$list");$number = count($list);sort($list);while($i < $number) {$list[$i] = trim($list[$i]);$i++;}$i=0;while ($i < $number) {echo "$list[$i]<p />";$i++;}$i = 0;while ($i < $number) {$str[$i] = preg_split('//',$list[$i]);sort($str[$i]);$i++;} print_r($str);echo "<p /><p />";$i = 0;while($i < $number) {$a = $i;$count[$i] = count($str[$i]);$i++;}$i = 0;$a = 0;while ($i < $number) {foreach($str[$i] as $alpha) {$alphat = $alphat . $alpha;echo $alpha;}echo "<p />";$i++;} echo "<p /><p /><p />";print_r($alphat);?>

Link to comment
Share on other sites

i've never used preg_split(), but on the manual and in an example there it says to use the PREG_SPLIT_NO_EMPTY flag to skip empty strings from being returned. http://php.net/manua....preg-split.phpI'd also recommend using str_split() instead for that. if you want $alphat to be an array of strings, the last part could be changed to

$i = 0;$alphat = array(); while ($i < $number) {	$alphat[$i] = ''; 	foreach($str[$i] as $alpha) {		$alphat[$i] .= $alpha;	} 	$i++;} print_r($alphat);

or easier with implode:

$i = 0;$alphat = array(); while ($i < $number) {	$alphat[$i] = implode('', $str[$i]); 	$i++;} print_r($alphat);

Link to comment
Share on other sites

Alright thanks. Script is working well now. Here's the final script, just have to add my large list of words now.

<?php// my list got them into alphabetical order$list = "helpthisscriptmightelwoodatherus";// test scrambled words, got it into alphabetical order, explode as well$scrambled = $_GET['scrambled'];$scrambled = explode("\n", $scrambled);$number2 = count($scrambled);// explode list, count and sort$list = explode("\n","$list");$number = count($list);sort($list);// trim list, not really working I don't think.$i = 0;while($i < $number) {$list[$i] = trim($list[$i]);$i++;}// trim $scrambled$i2 = 0;while($i2 < $number2) {$scrambled[$i2] = trim($scrambled[$i2]);$i2++;}// echo my sorted list to check values$i=0;while ($i < $number) {echo "$list[$i]<p />";echo "$scrambled[$i]<p />";$i++;}// split $list into individual characters and sort them -- got rid of empty space problem// thanks to JamesB.$i = 0;while ($i < $number) {$str[$i] = preg_split('//',$list[$i],-1,PREG_SPLIT_NO_EMPTY);sort($str[$i]);$i++;}// Do the same thing with scrambled list$i2 = 0;while ($i2 < $number2) {$scrambled_char[$i2] = preg_split('//',$scrambled[$i2],-1,PREG_SPLIT_NO_EMPTY);sort($scrambled_char[$i2]);$i2++;}// print my alphabetical arrays, scrambled an unscrambled.print_r($scrambled_char);echo "<p />";print_r($str);echo "<p /><p />";// implode my alphabetical arrays and print the results.$i = 0;while ($i < $number) {$alphalist[$i] = implode($str[$i]);echo "<p />";$i++;}echo "<p /><p /><p />";print_r($alphalist);$i2 = 0;while ($i2 < $number2) {$alphascrambled[$i2] = implode($scrambled_char[$i2]);echo "<p />";$i2++;}print_r($alphascrambled);// Check for matches$i = 0;while($i < $number) {$i2 = 0;while($i2 < $number2) {similar_text($alphalist[$i],$alphascrambled[$i2], $percent);if($percent == '100') {$unscrambled[$i2] = $list[$i];}$i2++;}$i++;}print_r($unscrambled);?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...