How to retrieve values from a multi-d array in PHP?

Biscuit

New Member
#1
Here is an array from an user generated course-creation system. What would be the best way to retrieve values associated with [titreSection], [lessonTitle] and [lessonContent] keys ?

Main goal here is to allow the user to create sections that contains a title with several lessons and content associated to a section. The order and logic remains the same. Only the number of sections and [content] will change depending the structure of the created course.

Here is the foreach loop with the results
Mã:
$course = $_POST['section'];

foreach ($course as $key=>$value){

    foreach ($value as $key1=>$value1)

        foreach ($value1 as $key2=>$value2)

            foreach ($value2 as $key3=>$value3)
               echo $key3 . '==>' . $value3 . '<br >';

}
From this result, how can i print out all the '[titreSection]' only ?
Mã:
titreSection==>Section 1
lessonTitle==>Lesson 1
lessonContent==>Contenu 1
lessonTitle==>Lesson 2
lessonContent==>Contenu 2
titreSection==>Section 2
lessonTitle==>Lesson 3
lessonContent==>Contenu 3
lessonTitle==>Lesson 4
lessonContent==>Contenu 4




Array
(
    [1] => Array
        (
            [titre] => Array
                (
                    [1] => Array
                        (
                            [titreSection] => Section 1
                        )

                )

            [contenu] => Array
                (
                    [1] => Array
                        (
                            [lessonTitle] => Lesson 1
                            [lessonContent] => Contenu 1
                        )

                    [2] => Array
                        (
                            [lessonTitle] => Lesson 2
                            [lessonContent] => Contenu 2
                        )

                )

        )

    [2] => Array
        (
            [titre] => Array
                (
                    [1] => Array
                        (
                            [titreSection] => Section 2
                        )

                )

            [contenu] => Array
                (
                    [3] => Array
                        (
                            [lessonTitle] => Lesson 3
                            [lessonContent] => Contenu 3
                        )

                    [4] => Array
                        (
                            [lessonTitle] => Lesson 4
                            [lessonContent] => Contenu 4
                        )

                )

        )

)
 

Admin

Administrator
Thành viên BQT
#2
Something like this could probably start you off in the right direction. I've assumed your data is in an array called $sections. Note that you have inconsistent index values in your data (lessonTitleand titreLesson) I've assumed they are all titreLesson.
Mã:
foreach ($sections as $section) {
    echo "<h2>{$section['titre'][1]['titreSection']}</h2>\n";
    foreach ($section['contenu'] as $lesson) {
        echo "<h3>{$lesson['titreLesson']}</h3>\n";
        echo "<p>{$lesson['lessonContent']}</p>\n";
    }
}
Output
Mã:
<h2>Section1</h2>
  <h3>Lesson1</h3>
    <p>Contenu1</p>
  <h3>Lesson2</h3>
    <p>Contenu2</p>
<h2>Section2</h2>
  <h3>Lesson3</h3>
    <p>Contenu3</p>
  <h3>Lesson4</h3>
    <p>Contenu4</p>
Demo on 3v4l.org
 
OP
OP
B

Biscuit

New Member
#3
Thanks, my initial foreach loop was not detailed enough to retrieve specific data. thanks again.
 
Top