注意:未定义偏移量:0

3

我一直在开发一些涉及cURL操作和抓取特定URL内容的应用程序。

然后进行一些计算和处理所抓取的内容。

目前我遇到的问题与未定义的数组索引有关。

这里有一些出现问题的函数:

{注意:Undefined offset: 0 in D:\xampp\htdocs\Alps-Phase2\alps\include\alpsOP\scrap-process-request2.php on line 263}

还有更多类似的函数。

function getDomainName($objScrap)
 {
try
{
    $result = $objScrap->getDomainName();

    return $result;  //Notice: Undefined offset: 0 
}
catch( Exception $e)
{
    echo "Error in getDomainName !";
    return FALSE;
}

  }

  function getDataForDomainName($objScrap)
  {
try
{
    $result = $objScrap->checkForKeywordInDomain();

    return $result[0];    // Notice: Undefined offset: 0 
}
catch( Exception $e)
{
    echo "Error in getDataForDomainName !";
    return FALSE;
}
 }

function getDensityForDomainName($objScrap){
try
{
    $result = $objScrap->getDomainDensity();
    return $result[0];        // Notice: Undefined offset: 0 
}
catch( Exception $e)
{
    echo "Error in getDensityForDomainName !";
    return FALSE;
}
  }

一些调用的函数定义:
   function getDomainDensity()
  {
    $result=$this->getDomainName();
    return $this->getKeywordDensity($result);
  }
   function getDomainName()
  {
    preg_match($this->_regExpDomain,$this->_url,$match);

    if($match != NULL)
    return $match[2];
    else
    return array(
    0=> 'Please check URL '.$this->$_url.' [Domain Name]',
    'error' => 'Please check URL '.$this->$_url.' [Domain Name]'
                            );
  }



   function getKeywordDensity(&$subject)
    {
    $splitKeywordCountTotal_len=0;
    $splitKeywordCount = array();
    $resultArray = array();

      for($count_i=0;$count_i<count($this->_keywords);$count_i++)
        {


        $splitKeyword = $this->splitKeyword($this->_keywords[$count_i]);

        $splitKeywordCount=0;
        $splitKeywordCount = $this->prepareResultArray($subject,NULL,$splitKeyword);

        $matchedKeywordCharacterCount=0;
        $f=0;

        foreach ($splitKeywordCount as $val=>$key)
        {
            $splitKeywordCount[$f][2]=strlen($key[0]);

            $splitKeywordCount[$f][3]=$key[1]*strlen($key[0]);
            $matchedKeywordCharacterCount=$matchedKeywordCharacterCount+$splitKeywordCount[$f][3];
            $f++;

        }

        $totalWordsInVisibleContent = $this->getNumberOfWordsInSubject($subject);
        $f=0;
        $totalWordsInVisibleContent_len=0;
        foreach ($totalWordsInVisibleContent as $val=>$key)
        {
            $totalWordsInVisibleContent_len=$totalWordsInVisibleContent_len+strlen($key);
        }

        $splitKeywordCountTotal = 0;
        for($count_j=0;$count_j < count($splitKeywordCount);$count_j++)
        {
            $splitKeywordCountTotal = $splitKeywordCountTotal + $splitKeywordCount[$count_j][1];
            $splitKeywordCountTotal_len = $splitKeywordCountTotal_len + $splitKeywordCount[$count_j][2];

        }

        $resultArray[$count_i]      =   array();
        $resultArray[$count_i][0]   =   $this->_keywords[$count_i];
        $resultArray[$count_i][1]   =   $matchedKeywordCharacterCount/ ($totalWordsInVisibleContent_len);
        $resultArray[$count_i][2]   =   $splitKeywordCountTotal;
        $resultArray[$count_i][3]   =   $matchedKeywordCharacterCount;
        $resultArray[$count_i][4]   =   $totalWordsInVisibleContent;
        $resultArray[$count_i][5]   =   $splitKeywordCountTotal_len;
        $resultArray[$count_i][6]   =   $totalWordsInVisibleContent_len;

    }

    return $resultArray;
}

此外,我计划为同一应用程序运行50万个URL。如果这些通知继续出现,我的应用程序将面临性能问题。所以各位,需要帮助解决这个问题。
*对于代码的草拟很抱歉...我是新手,不知道如何使用结构.. :(

2
var_dump($result) 在出现未定义的偏移量时会显示什么?要么数组没有 0 键,要么它的键不是整数(例如哈希)。 - Marc B
2
你的数组没有索引0,尽管你期望它们有。这是你的逻辑错误。 - deceze
@MarcB 嘿,var_dump($result) 函数会返回我们传入其中的任何网站URL链接的内容。这基本上是整个网站的cURL响应。 - Hemant Shekhawat
@deceze,你能告诉我如何将索引[0]添加到我的数组中吗? - Hemant Shekhawat
2个回答

0
我今天刚遇到了这样的问题!我使用的是PHP7.3.5,我得到了相同的错误,经过搜索,我找到了解决方案:

不要使用:

try
{
    $result = $objScrap->checkForKeywordInDomain();
    return $result[0];    // Notice: Undefined offset: 0 
}

更改为:

try
{
    $result = $objScrap->checkForKeywordInDomain();

    if(isset($result[0])){
      return $result[0];
   }
}

希望这能帮到任何人 }else{ :)


0

关闭HTML错误

在顶部包含以下内容

error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'Off'); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接