Deepcrawl获取数据:Undefined Offset 1

问题描述 投票:1回答: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]时,它可以正常工作,但它根本没有得到任何数据?如果你看到导致这种情况的原因,下面的帖子真的会有所帮助。谢谢! :)

php arrays laravel api
1个回答
1
投票

首先,将你的$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]索引,它可能甚至没有进入这个...我至少会初始化索引,如果和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;
}
© www.soinside.com 2019 - 2024. All rights reserved.