簡體   English   中英

PHP中的數組到字符串轉換錯誤

[英]Array to string conversion error in PHP

我有以下代碼來回溯錯誤..但是

$traces = debug_backtrace();

foreach ($traces as $k => $v)
{
    if ($v['function'] == 'include' 
        || $v['function'] == 'include_once' 
        || $v['function'] == 'require_once' 
        || $v['function'] == 'require')
    {
        $args = ''; 
        if (isset($v['args']) && is_array($v['args']))
        {
            $size = count($v['args']);
            foreach ($v['args'] as $key => $arg)
            {
                $args .= $v['args'][$key];
                if($key < $size)
                {
                    $args .= ', ';
                }
            }
        }

        $traces .= '#' . $k . ' ' 
                 . $v['function']
                 . '('.$args.') called at ['
                 . $v['file'].':'.$v['line'].']';
    }
    else
    {
        $function = (array_key_exists('function',$v)) ? 
                        $v['function'].'() ' : 'function_name';
        $file     = (array_key_exists('file',$v)) ? 
                        $v['file'] : 'file_name';
        $line     = (array_key_exists('line',$v)) ? 
                        $v['line'] : 'line';
        $traces  .= "#{$k} $function called at {$file}:{$line}\n";//This line giving me notice...

    }


}

我在這里注意到將數組轉換為字符串

$traces .= "#$k $function called at $file:$line\n";

我實際上想將此數組轉換為字符串。 有沒有可以在不通知我的情況下進行轉換的方法或函數...

我該如何糾正?

您開始於:

foreach($traces as $k=>$v) <- $traces here is an array

然后你嘗試去做

$traces.= "xxx" <- $traces here is handled as a string

我寧願定義一個$ tracestr字符串來聚合文本內容。

您沒有正確創建數組

 $args .= $v['args'][$key];

您正在創建一個字符串。

 $args = array(); 
                if(isset($v['args']) && is_array($v['args']))
                {
                    $size = count($v['args']);
                    foreach ($v['
                    args'] as $key => $arg)
                    {
                        array_push($args,$v['args'][$key]);
                       // some of your code
                }
$trace = debug_backtrace();
foreach($traces as ...)

這里有問題。 $ trace是一個調試Backtrace數組。 當你foreach($ traces) ...這似乎是不確定的。 然后,您將附加到$ traces后面,這應該算是一個非標量。

只需正確命名變量並命名不同即可!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM