繁体   English   中英

从PHP中的多维数组检索数据

[英]retrieving data from a multidimentional array in php

我这里有一个动态数组。 targetCourse数组包含stepNumber,stepTitle,description和courseId。 courseId同样是动态的,包含id,coursePrice和courseImage字段。

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"
      }
    }
  }
} 

我如何遍历它来存储这样的数据

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

如果基本数组的结构和键保持不变,那么您所需要的只是解决方法。 我将基本数组作为$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']
        )
    )
);

它将输出

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

        )

)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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