未定义:使用排序算法偏移错误消息


Getting Undefined:Offset error messages with Sorting Algorithm

所以当我运行这段代码时,我得到了很多这样的错误。我打算放弃,只使用PHP内置的排序函数。但我希望有人能看到这里的问题。请参阅下面的代码。如果你看不清,先说声抱歉。

数组输入与print_r输出完全符合预期,但实际的排序算法无法工作,无论我尝试什么。底部的两个注释函数用于不同的试验。

<?php
//this function will pull a string from a txt file and pass characters to an array
function strToArray($file){
    if ($handle = fopen($file, 'r')){
        $string = fread($handle, filesize($file)); 
        fclose($handle);
    }
    $strArray = str_split(preg_replace('/'s+/', '', $string)); //regex in preg_replace gets rid of all whitespaces; str_split converts string to array
    $arrLen = array_count_values($strArray); 
    return $arrLen;
}
$arrayWithVal = strToArray("filetest.txt"); //intermediary to pass into next function
print_r($arrayWithVal); //see what I have so far
echo "<hr />";
$newArray = $arrayWithVal;
for ($i = 1; $i < count($newArray); $i++){
    for ($j = $i-1; $j >= 0; $j--){
        if ($newArray[$j] > $newArray[$j+1]){ //if value on left is bigger than current value
            $oldValue = $newArray[$j+1];
            $newArray[$j+1] = $newArray[$j];
            $newArray[$j] = $oldValue;
            //return $newArray;
        }
        else {
            break; //if value on left is smaller, skip to next position
        }
    }            
}

print_r($newArray); //END
/*
function insertionSort($array){
    $newArray=$arrayWithVal;
    for($j=1; $j < count($newArray); $j++){  
        $temp = $newArray[$j];  
        $i = $j;  
        while(($i >= 0) && ($newArray[$i-1] > $temp)){  
            $newArray[$i] = $newArray[$i-1];  
            $i--;  
        }  
        $newArray[$i] = $temp;  
    }  
    return $array;
}
*/
/*
function insertionSort($arrData){
    for ($i=1;$i<count($arrData);$i++){
        for ($j=$i-1;$j>=0;$j--){
            if ($arrData[$j]>$arrData[$j+1]){ //if value on left is bigger than current value
                $oldValue = $arrData[$j+1];
                $arrData[$j+1] = $arrData[$j];
                $arrData[$j] = $oldValue;
            }
            else {
                break; //if value on left is smaller, skip to next position
            }
        }            
     }
     return $arrData;
}
*/
?>

EDIT:我还应该提到,在它返回错误之后,它打印与第一个print_r输出相同的数组。

您正在使用array_count_values($strArray)函数,该函数将返回一个数组,使用$strArray的值作为键,并使用$strArray中的频率作为值。

所以对于例子:$arrayWithVal将为:

array('some_word'=>3,'other_word'=>4);

你正在复制这个数组到$newArray,然后你试图访问$newArray与数字索引:$newArray[$j+1],而$newArray是一个关联数组。

这就是为什么你得到未定义的偏移错误。

你的问题的确切工作代码可以是:

//this function will pull a string from a txt file and pass characters to an array
function strToArray($file){
    if ($handle = fopen($file, 'r')){
        $string = fread($handle, filesize($file)); 
        fclose($handle);
    }
    $strArray = str_split(preg_replace('/'s+/', '', $string)); //regex in preg_replace gets rid of all whitespaces; str_split converts string to array
    $arrLen = array_count_values($strArray); 
    return $arrLen;
}
$arrayWithVal = strToArray("filetest.txt"); //intermediary to pass into next function
print_r($arrayWithVal); //see what I have so far
$newArray = $arrayWithVal;
$test = array();
foreach($newArray as $v){
    $test[] = $v;
}
for ($i = 1; $i < count($test); $i++){
    for ($j = $i-1; $j >= 0; $j--){
        if ($test[$j] > $test[$j+1]){ //if value on left is bigger than current value
            $oldValue = $test[$j+1];
            $test[$j+1] = $test[$j];
            $test[$j] = $oldValue;
            //return $newArray;
        }
        else {
            break; //if value on left is smaller, skip to next position
        }
    }            
}
$result = array();
foreach($test as $k => $v){
    $keys_array = array_keys($newArray, $v);
    foreach($keys_array as $key){
        $result[$key] = $v;
    }
}
print_r($result);// to see $result array

如果你的$newArray是:

Array
(
    [some] => 4
    [r] => 3
    [w] => 6
    [t] => 1
    [a] => 8
    [hell] => 4
)

$result数组将:

Array
(
    [t] => 1
    [r] => 3
    [some] => 4
    [hell] => 4
    [w] => 6
    [a] => 8
)

如果你正在学习PHP,这是一个很好的方法,否则你应该只使用内置PHP函数,以获得更好的时间性能。

希望对大家有所帮助