简体   繁体   中英

retrieving data from a multidimentional array in php

I have here an array which is dynamic. The targetCourse array contains stepNumber, stepTitle, description & courseId. The courseId is again dynamic and contains id, coursePrice & courseImage fields.

array(1) {
  [0]=>
  array(4) {
    ["stepNumber"]=>
    string(0) ""
    ["stepTitle"]=>
    string(0) ""
    ["description"]=>
    string(11) "<p><br></p>"
    ["courseId"]=>
    array(3) {
      [0]=>
      array(4) {
        ["courseTitle"]=>
        string(4) "Java"
        ["id"]=>
        string(18) "616716226880155648"
        ["coursePrice"]=>
        string(1) "0"
        ["courseImageUrl"]=>
        string(43) "/images/613975354956722176/HENkLeDExX_t.jpg"
      }
      [1]=>
      array(4) {
        ["courseTitle"]=>
        string(10) "C Language"
        ["id"]=>
        string(18) "616692519117860864"
        ["coursePrice"]=>
        string(1) "0"
        ["courseImageUrl"]=>
        string(43) "/images/613975354956722176/b3JH1zvo3b_t.jpg"
      }
      [2]=>
      array(4) {
        ["courseTitle"]=>
        string(3) "PHP"
        ["id"]=>
        string(18) "616696505808007168"
        ["coursePrice"]=>
        string(1) "0"
        ["courseImageUrl"]=>
        string(43) "/images/613975354956722176/Ms7gZKuJRg_t.jpg"
      }
    }
  }
} 

How can i traverse through it to store data like this

'targetCourse' => [
            [
                'stepNumber' => $this->stepNumber,
                'stepTitle' => $this->stepTitle,
                'description' => $this->description,
                'courseId' => [
                    [
                        'courseImageUrl' => $this->courseImageUrl,
                        'courseTitle' => $this->courseTitle,
                        'coursePrice' => $this->coursePrice,
                        'id' => $this->id
                    ]
                ]
            ]
]

No exactly what you are looking for but a work around, if the base array structure and keys remain same. I took the base array as $result

array(
    'targetCourse' => array(
        'stepNumber' => $result[0]['stepNumber'],
        'stepTitle' => $result[0]['stepTitle'],
        'description' => $result[0]['description'],
        'courseId' => array(
            'courseImageUrl' => $result[0]['courseId'][0]['courseImageUrl'],
            'courseTitle' => $result[0]['courseId'][0]['courseTitle'],
            'coursePrice' => $result[0]['courseId'][0]['coursePrice'],
            'id' => $result[0]['courseId'][0]['id']
        )
    )
);

It will output

Array
(
    [targetCourse] => Array
        (
            [stepNumber] => 
            [stepTitle] => 
            [description] => 
            [courseId] => Array
                (
                    [courseImageUrl] => /images/613975354956722176/HENkLeDExX_t.jpg
                    [courseTitle] => Java
                    [coursePrice] => 0
                    [id] => 616716226880155648
                )

        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM