Donz
Donz

Reputation: 15

Foreach loop Multidimensional Array

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 :)

Upvotes: 1

Views: 210

Answers (2)

gia
gia

Reputation: 757

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

Upvotes: 0

Kevin
Kevin

Reputation: 41893

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.

Upvotes: 2

Related Questions