Clementine
Clementine

Reputation: 131

PHP: Arrays displayed as string

I'm trying to parse xml result with php (DOM) :

stdClass Object
  (
 [GetBilletResult] => Array
  (
 [0] => stdClass Object
    (
        [tabGrilleHoraire] => stdClass Object
            (
                [tabDetailTarifArticle] => stdClass Object
                    (
                        [sNomArticle] => ARTICLE1
                        [tabDetail] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [sDetail] => Liste Pax [Âge:19]
                                        [sAgePax] => 19;
                                        ...
                                    )

                                [1] => stdClass Object
                                    (
                                        [sDetail] => Prix de Base
                                        [sAgePax] => 18;
                                        ...
                                    )

My code so far:

$processed = array();
foreach( $billets as $GetBilletResult )
    {
        $sNomProduit = $GetBilletResult->getElementsByTagName( "sNomProduit" )->item(0)->nodeValue;
        $sNomArticle = $GetBilletResult->getElementsByTagName( "sNomArticle" )->item(0)->nodeValue;
        $tabDetail = $GetBilletResult->getElementsByTagName( "tabDetail" );

        if (!isset($processed[$sNomProduit])) {
            $processed[$sNomProduit] = array();
        }
        $processed[$sNomProduit][] = array(
                                           'sNomArticle' => $sNomArticle,
                                           'tabDetail' => $tabDetail,
                                           );
    }

The loop to display the results (articles are regrouped by product):

foreach ($processed as $sNomProduit => $list) {
    echo "<h3> ".$sNomProduit."</h3>";
    foreach ($list as $item) {
        echo "<h5> ".$item['sNomArticle'] . "</h5>";
        foreach ($item['tabDetail'] as $node) {
            var_dump ($node->nodeValue);
        }
    }
}

Output (The arrays under "tabDetail" array are displayed as strings)

PRODUIT A

ARTICLE A1

string 'Liste Pax : Pax n°1 [Âge:19]19;ADULTE(12-59.99)00000' (length=54)

string 'Prix de Base240300000' (length=21)

...

Upvotes: 3

Views: 118

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

In this part of your code:

foreach ($item['tabDetail'] as $node) {
    var_dump ($node->nodeValue);
}

If there are more child nodes under tabDetail, using $node->nodeValue will just get the textContent of the node and its descendants. If you want to print the items separately, you should be able to iterate over the childNodes and output their values.

foreach ($item['tabDetail'] as $node) {
    foreach ($node->childNodes as $child) {
        echo $child->nodeValue;
    }
}

Or get the value of specific nodes you want as you are doing in your earlier code:

foreach ($item['tabDetail'] as $node) {
    echo $node->getElementsByTagName( "sDetail" )->item(0)->nodeValue;
}

Upvotes: 3

Related Questions