1

I am working with an multidimensional array which I am receiving from a third party source so can not change the layout of it. Its for a collge and I want to loop the module names but not the ID's.

When using this foreachloop

<?php echo "<ul>";
                foreach($array['Course']['Modules']['Module'] as $key => $value)
                {
                    foreach($value as $keys => $values)
                    {
                        echo "<li>";
                            foreach($values as $thirdkey => $thirdvalues)
                            {
                            echo $thirdvalues . " " ;
                            }
                        echo "</li>";
                }
                    }
                echo "</ul>"
            ?>

I get the following which is fine but I don't need the moduleID:

003646 T.I.G. WELDING ALUMINIMUM INTERMEDIATE
003644 T.I.G. WELDING STAINLESS STEEL INTERMEDIATE
003633 MIG WELDING INTERMEDIATE
003552 MANUAL METAL ARC WELDING INTERMEDIATE
000016 MOUNTING OF ABRASIVE WHEELS
003299 MACHINE TOOLS (WELDING)
000001 INDUCTION
000002 CAREER PLANNING AND JOB SEEKING SKILLS
000029 OXY-ACETYLENE CUTTING
003757 THEORY OF WELDING
000003 IN-COMPANY
000664 SAFEPASS

This is my array, can anyone help me return just the Module Title??? Array

([Certification] => IT Systems Support - PC Maintenance (L1)
 [Modules] => Array
            (
                [Module] => Array
                    (
                        [0] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [ModuleID] => 004839
                                        [Title] => IT SYSTEMS SUPPORT- PC MAINTENANCE
                                    )

                            )

                        [1] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [ModuleID] => 007685
                                        [Title] => COMPTIA A+ - 220 801
                                    )

                            )

                        [2] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [ModuleID] => 007684
                                        [Title] => COMPTIA A+ - 220-802
                                    )

                            )

                        [3] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [ModuleID] => 004757
                                        [Title] => PREPARING FOR WORK
                                    )

                            )

                    )

            )

    )

Thank in advance :)

1
  • 2
    if you don't want to id, then just directly point to Title Commented Mar 30, 2015 at 1:50

2 Answers 2

2

You could just directly point to it.

foreach($array['Course']['Modules']['Module'] as $key => $value) {
    echo "<li>{$value['@attributes']['Title']}</li>";
}

Sidenote: Most likely this is a SimpleXML object, fed into a json encode/decode.

$array = json_decode(json_encode($xml), true); // actually don't need to do this one

I'd suggest just use SimpleXML all through out as it is already capable of getting all those values.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ghost, thank you for this I knew they had to be a simpler way, and your spot on it is a SimpleXml object.
0

You didn't care to try $thirdvalues["Title"] right?, looks like a mess but I don't see why it wouldn't work if you got it to print that.

can also do if( $key == "Title" ) echo $value

Comments

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.