繁体   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