繁体   English   中英

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

[英]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;
}
*/
?>

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

您正在使用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函数以获得更好的时间性能。

我希望它有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM