2015-11-18 84 views
1

我试图过滤一些日志,就像我需要它们,并试图使其动态。我有一些域名,我试图从中过滤一些东西,并且它们都像我想要的那样工作 - 但现在我更改了域名,现在我的代码不再工作了。它说一个变量没有被定义。为什么PHP告诉我这个变量没有定义?

 $sp_bots = shell_exec("grep bot | awk '{print $12}' /var/www/laravel/logs/vhosts/zahnmedizinisches-fachzentrum-berlin.de.log"); 
    $array_sp_bots = explode("\n", $sp_bots); 
    $all_bots = array(); 
    foreach($array_sp_bots as $bots){ 
     if(strpos($bots, "bot")){ 
      $all_bots[] = $bots; 
     } 
    } 
    # count values of strings in array 
    if (!empty($all_bots)) { 
     $bots = array_count_values($all_bots); 
     arsort($bots); 
     $mostOccuring = max(array_count_values($all_bots)); 
     $bot_keys = array_keys($bots); 
     #number of total bots 
     $count_bots = count($all_bots); 
    } 

,并在我的回报:

return view('/domains/data', [ 

     'count_bots' => $count_bots, 
     'bot_keys' => $bot_keys, 
     'mostOccuring' => $mostOccuring, 
    ]); 

但在我回报所有三个变量是不确定的..任何人都知道这是为什么?

+0

什么的$更改域后sp_bots输出? – Sabari

+1

定义$ all_bots,如$ all_bots = array();上面的foreach – Surace

+0

因为你从来没有定义'$ all_bots',只是假设当你',它已经存在$ all_bots [] = $机器人;' –

回答

3

您必须初始化数组作为循环之前的空数组:

$all_bots = array();   //init the empty array 

foreach($array_sp_bots as $bots) 
{ 
    if(strpos($bots, "bot")) 
    { 
     $all_bots[] = $bots; //here you can add elements to the array 
    } 
} 
你的情况

,如果循环将不会执行至少一次,该变量$all_bots将是不确定的

EDIT

循环后,处理的情况下,当数组为空做财产以后这样的:

//if there is some element in all_bots... 
if (! empty($all_bots)) 
{ 
    # count values of strings in array 
    $bots = array_count_values($all_bots); 
    arsort($bots); 
    $mostOccuring = max(array_count_values($all_bots)); 
    $bot_keys = array_keys($bots); 
    #number of total bots 
    $count_bots = count($all_bots); 
} 
//handle the case the variable all_bots is empty 
else 
{ 
    $bots = 0; 
    $count_bots = 0; 
    $bot_keys = 0; 
    $mostOccuring = 0; 
} 

EDIT2

您的收益不确定,因为当所有$all_bots是空的没有设置他们的变量。检查我上面的编辑,我已经将它们添加到if语句中。但是你必须根据你的需求在你的应用程序中处理这种情况,这样想:当$all_bots为空时,这些变量应该包含什么?然后,因为改变它并不在循环中执行域后分配在if语句

+0

是的,因为你试图从一个空数组中获得最大值,请检查我的编辑 – Moppo

+0

好吧,这工作正常谢谢:)但知道我有问题,它是赛义德:未定义变量:count_bots - 但我在这里定义它: $ count_bots = count($ all_bots); 是因为$ all_bots或为什么? – Evolution48

+0

奇怪的是'$ count_bots'不能被定义,你在哪里访问变量? – Moppo

2

它发生的值的变量。与尝试 -

$all_bots= array(); // Define an empty array 
foreach($array_sp_bots as $bots){ 
    if(strpos($bots, "bot")){ 
     $all_bots[] = $bots; 
    } 
} 
# count values of strings in array 
$bots = array_count_values($all_bots); 

如果$array_sp_bots为空,则不会执行循环& $all_bots不会被定义。在这种情况下,计数将是0

或者可能可能要增加一些检查为 -

if(empty($all_bots)) { 
    // Some error message 
} else { 
    # count values of strings in array 
    $bots = array_count_values($all_bots); 
    arsort($bots); 
    $mostOccuring = max(array_count_values($all_bots)); 
    $bot_keys = array_keys($bots); 
    #number of total bots 
    $count_bots = count($all_bots); 
} 
+0

谢谢,但现在我得到这个from php:max():数组必须包含至少一个元素 – Evolution48

+0

检查更新。应该只执行其余的代码,而不是空的。 –

+0

IAM回到我的变量,以我的看法,但所有3个变量是不确定的.. 'count_bots'=> $ count_bots, 'bot_keys'=> $ bot_keys, 'mostOccuring'=> $ mostOccuring, – Evolution48

相关问题