2012-11-07 56 views
0

我需要检查数组值,但是当数组是空的,我得到这个空管检查:错误:无法使用字符串数组深阵列在PHP

if (!empty($items[$i]['tickets']['ticket'][0]['price']['eur'])) { //do something } 

如何做是正确的偏移?

+0

你读过这一点:http://informationideas.com/news/2006/06/14/fatal-error-cannot- use-string-offset-as-an-array-in/ – George

+0

感谢您的链接。我读过它,但仍然不知道 – Gediminas

回答

2

您需要检查变量是否设置,然后如果它是数组,然后检查数组的元素是否设置。 if的语句将按顺序执行,并且当错误时将会中断。

if(isset($items) && is_array($items) && isset($items[$i]['tickets']['ticket'][0]['price']['eur'])) { 
    //jep it's there 
} 
0

或者只是尝试(额外sipmle变体):

if (!isset($items[$i]['tickets']['ticket'][0]['price']['eur'])) { 
    // do action 
}