簡體   English   中英

致命錯誤:無法將字符串偏移量用作數組-比較數組值的正確方法是什么

[英]Fatal error: Cannot use string offset as an array - What the right way to compare array value

我調用數組並嘗試比較這些值,我的語法有問題嗎?

foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
    $field = "100";
    if ($item["@attributes"]["tag"] == $field) {

    }
}

這是我的數組:

     array(1) { ["inf"]=> array(9) { ["hid"]=> string(13) "4754745675467" ["created_by"]=> string(6) "import" ["created_date"]=> string(11) "2017-01-01Z" ["last_modified_by"]=> string(13) "Update Record" ["last_modified_date"]=> string(11) "2018-01-2Z" ["originating_system"]=> string(3) "rrr" ["orid"]=> string(15) "1234565432167854" ["supp"]=> string(5) "false" ["rec"]=> array(3) { ["lead"]=> string(3) "500" ["field"]=> array(2) { [0]=> array(2) { ["@value"]=> string(5) "22333" ["@attributes"]=> array(1) { ["tag"]=> string(3) "001" } } [1]=> array(2) { ["@value"]=> string(3) "110" ["@attributes"]=> array(1) { ["tag"]=> string(3) "001" } } } ["dfield"]=> array(2) { [0]=> array(2) { ["subfield"]=> array(2) { ["@value"]=> string(2) "92" ["@attributes"]=> array(1) { ["cod"]=> string(1) "a" } } ["@attributes"]=> array(3) { ["ind1"]=> string(1) " " ["ind2"]=> string(1) " " ["tag"]=> string(3) "101" } } [1]=> array(2) { ["subfield"]=> array(3) { [0]=> array(2) { ["@value"]=> string(4) "ntft" ["@attributes"]=> array(1) { ["cod"]=> string(1) "b" } } [1]=> array(2) { ["@value"]=> string(5) "nthgfr" ["@attributes"]=> array(1) { ["cod"]=> string(1) "c" } } [2]=> array(2) { ["@value"]=> string(5) "test2" ["@attributes"]=> array(1) { ["cod"]=> string(1) "z" } } } ["@attributes"]=> array(3) { ["ind1"]=> string(1) "1" ["ind2"]=> string(1) " " ["tag"]=> string(3) "100" } } } } } } 


I compare `tag = 100` to a variable with value 100: `if ($item["@attributes"]["tag"] == $field)`

在此帖子的上次討論中使用的更改之后,我收到了這個數組:

array(1) { ["inf"]=> array(9) { ["hid"]=> string(13) "4754745675467" ["created_by"]=> string(6) "import" ["created_date"]=> string(11) "2017-01-01Z" ["last_modified_by"]=> string(13) "Update Record" ["last_modified_date"]=> string(11) "2018-01-2Z" ["originating_system"]=> string(3) "rrr" ["orid"]=> string(15) "1234565432167854" ["supp"]=> string(5) "false" ["rec"]=> array(3) { ["lead"]=> string(3) "500" ["field"]=> array(2) { [0]=> array(2) { ["@value"]=> string(5) "22333" ["@attributes"]=> array(1) { ["tag"]=> string(3) "001" } } [1]=> array(2) { ["@value"]=> string(3) "110" ["@attributes"]=> array(1) { ["tag"]=> string(3) "001" } } } ["dfield"]=> array(3) { [0]=> array(2) { ["subfield"]=> array(2) { ["@value"]=> string(2) "92" ["@attributes"]=> array(1) { ["cod"]=> string(1) "a" } } ["@attributes"]=> array(3) { ["ind1"]=> string(1) " " ["ind2"]=> string(1) " " ["tag"]=> string(3) "101" } } [1]=> array(2) { ["subfield"]=> array(3) { [0]=> array(2) { ["@value"]=> string(4) "ntft" ["@attributes"]=> array(1) { ["code"]=> string(1) "b" } } [1]=> array(2) { ["@value"]=> string(5) "nthgfr" ["@attributes"]=> array(1) { ["code"]=> string(1) "c" } } [2]=> array(2) { ["@value"]=> string(4) "test" ["@attributes"]=> array(1) { ["cod"]=> string(1) "z" } } } ["@attributes"]=> array(3) { ["ind1"]=> string(1) "1" ["ind2"]=> string(1) " " ["tag"]=> string(3) "100" } } ["subfield"]=> array(1) { [2]=> array(1) { ["@value"]=> string(12) "26A 1 2 test" } } } } } } 

您正在使用["@attributes"]$item不是“標准”數組。 它似乎是一個SimpleXMLElement對象。

嘗試使用以下代碼:

foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
    $field = "100";
    if ((string)$item["tag"] == $field) {
        var_dump("Equals") ;
    }
}

編輯

$field = "100";
foreach ($xml_record_ray['inf']['rec'] as $key_item => $item) {
    var_dump($key_item) ;
    if (is_array($item)) { 
        foreach ($item as $key_element => $element) {
            var_dump($key_element) ;
            if (!isset($element["@attributes"])) { echo("no attribute"); continue ; }
            if (!isset($element["@attributes"]['tag'])) { echo("no tag"); continue; }
            if ($element["@attributes"]["tag"] == $field) {
                var_dump("match") ;
            }
        }
    }
    else{
        echo "item is not an array" ;
    }
}

暫無
暫無

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

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