How can I declare and have multi dimensional object

Discussion in 'PHP' started by smilesquare, Jun 17, 2008.

  1. #1
    when I use simplexml_load_string , It return multi dimensional object.

    I made some test code like this, and it error, can anyone help me to correct the code, teach me how to declare and make this code alive, Thank you.

    $aaa = new stdClass();

    $aaa->cat[0] = "cat0";
    $aaa->cat[1] = "cat1";

    $aaa->cat[0]->name = "Hello 0";
    $aaa->cat[1]->name = "Hello 1";

    $aaa->cat[0]->Lastname = "Last 0";
    $aaa->cat[1]->Lastname = "Last 1";
     
    smilesquare, Jun 17, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    $string = <<<XML
    <?xml version='1.0'?>
    <document>
    <title>Forty What?</title>
    <from>Joe</from>
    <to>Jane</to>
    <family>
    <father>PHP</father>
    <mother>MYSQL</mother>
    <child>HTML</child>
    <brother>ASP</brother>
    </family>
    <body>
    I know that's the answer -- but what's the question?
    </body>
    </document>
    XML;


    $aaa = simplexml_load_string($string); //<---important line, always use simplexml_load_string.

    echo $aaa->body;
    echo $aaa->family->father;
    echo $aaa->family->brother;

    //You can change the property
    $aaa->body = 'Meowww';
    echo $aaa->body;
     
    php-lover, Jun 17, 2008 IP