3

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)

...

7
  • 2
    And the question is...? Commented Aug 11, 2016 at 16:31
  • 1
    You're using var_dump, is that intentional? Don't you just want echo? Commented Aug 11, 2016 at 16:33
  • The arrays under "tabDetail" array should be displayed as arrays not strings Commented Aug 11, 2016 at 16:34
  • @Don't Panic. This is not the problem. I'm using var_dump to check the result type. Replace it by echo doesn't fix the problem. Commented Aug 11, 2016 at 16:37
  • This is my actual sample code and tabDetail array structure. Commented Aug 11, 2016 at 17:07

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

9 Comments

@dont-panic. The tabDetail array : link. How can i save it like that, as array of arrays ?
You mean, instead of outputting the text, you want to save it to a variable?
Exactly, and keep the original structure.
I replaced echo $child->nodeValue; by $array1 = $child->nodeValue; array_push($arrayAll, $array1);. I can't add $child->nodeValue as keys
I would suggest $arrayAll[$child->nodeName] = $nodeValue; if you want to get the keys as well as the values. I think that should work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.