首页 > 解决方案 > Deepcrawl获取数据:未定义的偏移量1

问题描述

Deepcrawl.php

<?php 

$array_issues[] = 0;

$issues = array("thin_pages_basic","not_in_sitemaps_primary_indexable_basic","duplicate_titles_2_basic","pages_with_duplicate_titles_basic","max_external_links_basic","4xx_errors_basic");

for($i=0;$i<6;$i++){
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"Your Api key". $issues[$i]);

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Auth-Token:91PB_EP-LSaTVcm2bCjWyE9oLBEhGYSpvuW-gILR1eCEiK-VPL4YO40L8hTFTnepC8HjaLgn2C3AG6k3iv_-zg'));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output2 = curl_exec ($ch);
curl_close ($ch); 

$results = json_decode($server_output2);

$j=0;
// to get each object from an array of object
foreach((array)$results as $result){ 
  if($j==3)
  $array_issues[$i] = $result;

  $j++;
  }
} 
?>

在视图文件中显示

                 <tr>
                    <td style='color:#e86363'>" . $array_issues[0] . "</td>
                    <td style='width:50px'></td>
                    <td>Thin Pages</td>
                </tr>
                 <tr>
                    <td style='color:#e86363'>" . $array_issues[1] . "</td>
                    <td style='width:50px'></td>
                    <td>Primary Indexable Pages Not in Sitemaps </td>
                </tr>
                 <tr>
                    <td style='color:#e86363'>" . $array_issues[2] . "</td>
                    <td style='width:50px'></td>
                    <td>Duplicate Title Sets</td>
                </tr>
                 <tr>
                    <td style='color:#e86363'>" . $array_issues[3] . "</td>
                    <td style='width:50px'></td>
                    <td>Pages with Duplicate Titles</td>
                </tr>
                 <tr>
                    <td style='color:#e86363'>" . $array_issues[4] . "</td>
                    <td style='width:50px'></td>
                    <td>High External Linking</td>
                </tr>
                 <tr>
                    <td style='color:#e86363'>" . $array_issues[5] . "</td>
                    <td style='width:50px'></td>
                    <td>Broken Pages (4xx Errors)</td>
                </tr>

只是想知道当我在我的视图文件上声明我总是得到这个错误Undefined offset: 1

但是当我将数组从 [1-5] 更改为 [0] 时,它可以正常工作,但它根本没有得到任何数据?如果您看到是什么原因造成的,那么在下面发帖会很有帮助。谢谢!:)

标签: phparrayslaravelapi

解决方案


首先,将您的$array_issues声明更改为

$array_issues = array(); 

或者

$array_issues = [];

然后,我不知道您要在这几行中完成什么:

$results = json_decode($server_output2);

$j=0;
// to get each object from an array of object
foreach((array)$results as $result){ 
    if($j==3)
    $array_issues[$i] = $result;

    $j++;
}

但是,我认为这没有$array_issues[0]正确初始化您的索引,它甚至可能没有输入...我至少会在 if 和 foreach 之外初始化索引,例如:

$j=0;
// to get each object from an array of object
$array_issues[$i] = ""; // MAKING SURE THIS IS AT LEAST DEFINED.
foreach((array)$results as $result){ 
    if($j==3)
    $array_issues[$i] = $result;

    $j++;
}

另外,我认为您可以使用 $key => $value 语法而不是 $j 计数器来完成您想要做的事情......

$array_issues[$i] = ""; // MAKING SURE THIS IS AT LEAST DEFINED.
foreach((array)$results as $key => $result){ 
    if($key==3)
    $array_issues[$i] = $result;
}

推荐阅读