简体   繁体   English

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

[英]Getting Undefined:Offset error messages with Sorting Algorithm

So I'm getting a lot of these errors when I run this code. 因此,当我运行此代码时,我遇到了很多这些错误。 I'm about to give up and just use the sorting functions baked into PHP. 我即将放弃,只使用PHP中的排序功能。 But I would love if anyone could see the problem here. 但如果有人能在这里看到问题我会很高兴。 Please see below code. 请看下面的代码。 Sorry in advance if it's hard to read. 如果难以阅读,请提前抱歉。

The array input is fine as print_r outputs exactly as expected, but the actual sorting algorithm just won't work, no matter what I try. 数组输入很好,因为print_r输出完全符合预期,但无论我尝试什么,实际的排序算法都无法正常工作。 The two commented functions at the bottom were used in different trials. 底部的两个评论函数用于不同的试验。

<?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: I should also mention that after it returns the errors, it prints the the same array that the first print_r output. 编辑:我还应该提到,在它返回错误后,它会输出与第一个print_r输出相同的数组。

You are using array_count_values($strArray) function which will return an array using the values of $strArray as keys and their frequency in $strArray as values. 您正在使用array_count_values($strArray)函数,它将使用$strArray的值作为键返回一个数组,并将它们在$strArray的频率作为值返回。

So for ex: $arrayWithVal will be: 所以对于ex: $arrayWithVal将是:

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

You are copying this array into $newArray and then later on you are trying to access $newArray with numeric index : $newArray[$j+1] while $newArray is an associative array. 您正在将此数组复制到$newArray ,然后您尝试使用数字索引访问$newArray$newArray[$j+1]$newArray是一个关联数组。

That is why you are getting undefined offset error. 这就是为什么你得到未定义的偏移误差。

Exact working code for your problem can be : 您问题的确切工作代码可以是:

//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

If your $newArray is: 如果你的$newArray是:

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

$result array will be : $result数组将是:

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

Its good approach if you are learning PHP but otherwise you should just use inbuild php functions for better time performance. 如果你正在学习PHP,它的好方法,但你应该只使用inbuild php函数以获得更好的时间性能。

I hope it helps 我希望它有所帮助

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

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