簡體   English   中英

非法字符串偏移警告 PHP

[英]Illegal string offset Warning PHP

將我的 php 版本更新到 5.4.0-3 后,我收到一個奇怪的 PHP 錯誤。

我有這個數組:

Array
(
    [host] => 127.0.0.1
    [port] => 11211
)

當我嘗試像這樣訪問它時,我收到奇怪的警告

 print $memcachedConfig['host'];
 print $memcachedConfig['port'];


 Warning: Illegal string offset 'host' in ....
 Warning: Illegal string offset 'port' in ...

我真的不想只編輯我的 php.ini 並重新設置錯誤級別。

錯誤Illegal string offset 'whatever' in...通常意味着:您正在嘗試將字符串用作完整數組。

這實際上是可能的,因為字符串可以被視為 php 中的單個字符數組。 所以你認為 $var 是一個帶鍵的數組,但它只是一個帶有標准數字鍵的字符串,例如:

$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error

你可以在這里看到這個: http : //ideone.com/fMhmkR

對於那些試圖將錯誤的模糊性轉化為對它做些什么的人來回答這個問題,就像我一樣。

您試圖訪問一個string ,就好像它是一個數組一樣,鍵是一個string string不會理解。 在代碼中我們可以看到問題:

"hello"["hello"];
// PHP Warning:  Illegal string offset 'hello' in php shell code on line 1

"hello"[0];
// No errors.

array("hello" => "val")["hello"];
// No errors. This is *probably* what you wanted.

深入

讓我們看看這個錯誤:

警告:非法字符串偏移量'port' in ...

它說什么? 它說我們正在嘗試使用字符串'port'作為字符串的偏移量。 像這樣:

$a_string = "string";

// This is ok:
echo $a_string[0]; // s
echo $a_string[1]; // t
echo $a_string[2]; // r
// ...

// !! Not good:
echo $a_string['port'];
// !! Warning: Illegal string offset 'port' in ...

這是什么原因造成的?

出於某種原因,您需要一個array ,但您有一個string 只是混搭而已。 也許你的變量被改變了,也許它從來都不是一個array ,這真的不重要。

可以做什么?

如果我們知道我們應該有一個array ,我們應該做一些基本的調試來確定為什么我們沒有array 如果我們不知道我們是否會有一個arraystring ,事情就會變得有點棘手。

我們可以做的是各種檢查,以確保我們沒有像is_arrayissetarray_key_exists這樣的通知、警告或錯誤:

$a_string = "string";
$an_array = array('port' => 'the_port');

if (is_array($a_string) && isset($a_string['port'])) {
    // No problem, we'll never get here.
    echo $a_string['port'];
}

if (is_array($an_array) && isset($an_array['port'])) {
    // Ok!
    echo $an_array['port']; // the_port
}

if (is_array($an_array) && isset($an_array['unset_key'])) {
    // No problem again, we won't enter.
    echo $an_array['unset_key'];
}


// Similar, but with array_key_exists
if (is_array($an_array) && array_key_exists('port', $an_array)) {
    // Ok!
    echo $an_array['port']; // the_port
}

issetarray_key_exists之間有一些細微的區別。 例如,如果$array['key']值為null ,則isset返回false array_key_exists只會檢查密鑰是否存在

請嘗試這種方式......我已經測試了這段代碼......它有效......

$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);

這里有很多很棒的答案 - 但我發現我的問題要簡單得多。

我試圖運行以下命令:

$x['name']   = $j['name'];

我在$x['name']上收到這個illegal string錯誤,因為我沒有先定義數組。 因此,在嘗試將內容分配給$x[]之前,我放入了以下代碼行:

$x = array();

它奏效了。

這個問題有點晚了,但對於正在搜索的其他人:我通過使用錯誤的值(類型)進行初始化得到了這個錯誤:

$varName = '';
$varName["x"] = "test"; // causes: Illegal string offset

正確的方法是:

 $varName = array();
 $varName["x"] = "test"; // works

從 PHP 5.4 開始,我們需要傳遞函數期望的相同數據類型值。 例如:

function testimonial($id); // This function expects $id as an integer

調用此函數時,如果提供了這樣的字符串值:

$id = $array['id']; // $id is of string type
testimonial($id); // illegal offset warning

由於數據類型不匹配,這將生成非法偏移警告。 為了解決這個問題,您可以使用settype

$id = settype($array['id'],"integer"); // $id now contains an integer instead of a string
testimonial($id); // now running smoothly

在檢查數組之前,請執行以下操作:

if(!is_array($memcachedConfig))
     $memcachedConfig = array();

在我的情況下,我將 mysql_fetch_assoc 更改為 mysql_fetch_array 並解決。 解決需要 3 天時間 :-( 並且我的項目的其他版本使用 fetch assoc 運行。

就我而言,當我更改執行sql查詢的函數時,我解決了這個問題: return json_encode($array)然后: return $array

它對我有用:

我的測試代碼:

$var2['data'] = array ('a'=>'21','b'=>'32','c'=>'55','d'=>'66','e'=>'77');
foreach($var2 as $result)
{  
    $test = $result['c'];
}
print_r($test);

輸出: 55

檢查一下伙計們。 謝謝

只是使用

$memcachedConfig = array();

 print $memcachedConfig['host'];
 print $memcachedConfig['port'];


 Warning: Illegal string offset 'host' in ....
 Warning: Illegal string offset 'port' in ....

這是因為你從來沒有定義什么是 $memcachedConfig,所以默認情況下是由字符串而不是數組來處理的。

我通過使用 trim() 函數解決了這個問題。 問題在於空間。

所以讓我們試試

$unit_size = []; //please declare the variable type 
$unit_size = exolode("x", $unit_size);
$width  = trim ($unit_size[1] );
$height = trim ($unit_size[2] );

我希望這能幫到您。

我認為這條消息的唯一原因是因為目標數組實際上是一個像字符串等 (JSON -> {"host": "127.0.0.1"}) 變量的數組

對於 PHP

//Setup Array like so
$memcachedConfig = array(
  "host" => "127.0.0.1",
  "port" => "11211"
);

//Always a good practice to check if empty

if(isset($memcachedConfig['host']) && isset($memcachedConfig['port'])){

    //Some codes

    print_r ($memcachedConfig['host']);
    print_r ($memcachedConfig['port']);

}

只要確保檢查返回的值不為空。 所以這個例子是針對 PHP 的,所以找出如何檢查其他語言中的數組是否為空。

以防萬一它對任何人都有幫助,我收到此錯誤是因為我忘記對序列化數組進行反序列化。 這絕對是我會檢查它是否適用於您的情況的東西。

這是一個舊的,但萬一有人可以從中受益。 如果您的數組為空,您也會收到此錯誤。

就我而言,我有:

$buyers_array = array();
$buyers_array = tep_get_buyers_info($this_buyer_id); // returns an array
...
echo $buyers_array['firstname'] . ' ' . $buyers_array['lastname']; 

我改為:

$buyers_array = array();
$buyers_array = tep_get_buyers_info($this_buyer_id); // returns an array
...
if(is_array($buyers_array)) {
   echo $buyers_array['firstname'] . ' ' . $buyers_array['lastname']; 
} else {
   echo 'Buyers id ' . $this_buyer_id . ' not found';
}

就我而言:在獲取數據庫記錄時確保使用result_array(); 而不是row(); row_array();

暫無
暫無

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

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