A
A
astrotrain2016-01-28 20:06:04
PHP
astrotrain, 2016-01-28 20:06:04

How to use a SimpleXMLElement as an associative array key?

In general, there is such an xml document:

<Domains>
        <DomainData IsValid="true">
            <DomainName>mozilla.com</DomainName>
            <Values>
                <Data>
                    <Parameter>Cy</Parameter>
                    <Value xsi:type="CyApiData">
                        <Cy>12000</Cy>
                        <Yaca>0</Yaca>
                        <YaBarMirrow>0</YaBarMirrow>
                    </Value>
                </Data>
            </Values>
        </DomainData>

And here is an example from the official documentation, but in the lowest loop it is slightly modified to write the data I need into an associative array (domain = tits).
foreach($sessionData->Domains->DomainData as $domain)
    {
        // Доменное имя
        echo $domain->DomainName."<br/>";
        $str = $domain->DomainName;     
        foreach($domain->Values->Data as $data)
        {
            echo $data->Parameter."<br/>";
            echo $data->Value."<br/>";

            foreach(get_object_vars($data->Value) as $paramKey => $paramValue)
            {
                //echo "{$paramKey} = {$paramValue}"."<br/>";
                if( stristr($paramKey,"cy") )
                {
                    //$out_array["domain"][] = $domain->DomainName;
                    //$out_array["domain"]["cy"][] = $paramValue;
                    $out_array[$str][] = $paramValue;
                }


            }
        }
        echo "<br/>";

But for some reason I always get an error and an empty array in the output:
Warning: Illegal offset type in C:\xampp\htdocs\w\functions\domain.php on line 1
19

This element seems to be a string element, but apparently it is not so in reality, so the interpreter swears. What could be the matter and how to correctly write the data to the array? Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2016-01-28
@astrotrain

$domain->DomainName->__toString();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question