1

I have a nested array on this link Array Sample

I am using code below to parse this, but second and beyond depth it's returning nothing. However tried with recursive function.

printAllValues($ArrXML);

function printAllValues($arr) {
    $keys = array_keys($arr);
    for($i = 0; $i < count($arr); $i++) {
        echo $keys[$i] . "$i.{<br>";
        foreach($arr[$keys[$i]] as $key => $value) {
            if(is_array($value))
            {
                printAllValues($value);
            }
            else
            {
            echo $key . " : " . $value . "<br>";        
           }
        }
        echo "}<br>";
    }
}

What I am doing Wrong? Please help.

10
  • $key isn't an array, maybe $value is one Commented Jan 17, 2018 at 7:43
  • Sorry!! this was a typo mistake at the time of post. I have corrected, but still not working. Edited above is my final code and I want to iterate the complete array with key value pair dynamically Commented Jan 17, 2018 at 7:45
  • Please provide all the info needed to solve the problem in the question - external links can change at any time, making the question unreliable Commented Jan 17, 2018 at 7:50
  • It's an object, not an array. Commented Jan 17, 2018 at 8:00
  • @Cristik Size of given Array is large and was not able to post it here. That's why I provided it over external link. Is there is a way to put it here? Commented Jan 17, 2018 at 8:03

2 Answers 2

1

Version of J. Litvak's answer that works with SimpleXMLElement objects.

function show($array) {
    foreach ($array as $key => $value) {
        if (!empty($value->children())) {
            show($value);
        } else {
            echo 'key=' . $key . ' value=' . $value. "<br>";
        }
    }
}

show($ArrXML);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use recurcive function to print all values:

function show($array) {
    foreach( $array as $key => $value) {
        if (is_array($value)) {
            show($value);
        } else{
            echo 'key=' . $key . ' value=' . $value. "<br>";
        }
    }
}

show($ArrXML);

1 Comment

It's not working. Have you tested with the sample provided in my question? The code I am using is returning me the values but not beyond level 1

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.