简体   繁体   中英

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

I call to array and try to compare the values, is there something wrong in my syntax?

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

    }
}

This is my array:

     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)`

This array I received after changes that used from last discussion from this post:

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" } } } } } } 

You're using ["@attributes"] but $item is not a "standard" array. It seems to be a SimpleXMLElement object.

Try to use the following code :

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

edit

$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" ;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM