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

时间:2016-11-02 11:03:02

标签: php sorting

因此,当我运行此代码时,我遇到了很多这些错误。我即将放弃,只使用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;
}
*/
?>

编辑:我还应该提一下,在它返回错误之后,它会输出与第一个print_r输出相同的数组。

1 个答案:

答案 0 :(得分:0)

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

所以对于ex: $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,它的好方法,但你应该只使用inbuild php函数来获得更好的时间性能。

我希望它有所帮助