从Multidem阵列中提取数据

时间:2018-06-11 19:19:30

标签: php html arrays json api

我一直在尝试从一个大型多维数组中为每个学生提取以下数据(再次向下显示)

Data -> gender,
Data -> grade,
Data -> name -> first,
Data -> name -> last,
Data -> name -> middle,
Data -> student_number,
Data -> ID

我通过搜索尝试了各种选项,包括Slice,Splice和for循环。

所有尝试都遇到了所有或部分数据失败。我从来没有能够获得First,Middle和Last名称的第三个嵌套数据。

如何使用这样的大型多重阵列,并在foreach循环中提取上面列出的数据,以便将其导入数据库?我觉得它比我做的更简单。我没有包含任何代码,因为我还没有任何看起来有用的东西。

下面是一个示例数组。谢谢!

       Array
    (
        [data] => Array
            (
                [0] => Array
                    (
                        [data] => Array
                            (
                                [gender] => M
                                [dob] => 7/17/2008
                                [email] => 
                                [grade] => 2
                                [schools] => Array
                                    (
                                        [0] =>12345
                                    )

                                [school] => 12345
                                [created] => 2018-04-16T14:01:00.437Z
                                [name] => Array
                                    (
                                        [first] => Jacob
                                        [last] => Smith
                                        [middle] => Rabbitboom
                                    )

                                [location] => Array
                                    (
                                        [zip] => 
                                        [address] => 
                                        [city] => 
                                        [lat] => 
                                        [lon] => 
                                        [state] => 
                                    )

                                [district] => 123456
                                [last_modified] => 2018-04-16T14:01:00.437Z
                                [race] => 
                                [hispanic_ethnicity] => 
                                [graduation_year] => 
                                [student_number] => 1234567
                                [credentials] => Array
                                    (
                                        [district_username] => 
                                    )

                                [id] => 123456
                            )

                        [uri] => 
                    )
  [1] => Array
                    (
                        [data] => Array
                            (
                                [gender] => F
                                [dob] => 7/17/2008
                                [email] => 
                                [grade] => 2
                                [schools] => Array
                                    (
                                        [0] =>12346
                                    )

                                [school] => 12345
                                [created] => 2018-04-16T14:01:00.437Z
                                [name] => Array
                                    (
                                        [first] => Jason
                                        [last] => Smith
                                        [middle] => RobesPerrie
                                    )

                                [location] => Array
                                    (
                                        [zip] => 
                                        [address] => 
                                        [city] => 
                                        [lat] => 
                                        [lon] => 
                                        [state] => 
                                    )

                                [district] => 123456
                                [last_modified] => 2018-04-16T14:01:00.437Z
                                [race] => 
                                [hispanic_ethnicity] => 
                                [graduation_year] => 
                                [student_number] => 1234568
                                [credentials] => Array
                                    (
                                        [district_username] => 
                                    )

                                [id] => 123459
                            )

                        [uri] => 
                    )

1 个答案:

答案 0 :(得分:1)

最简单的方法可能是提取嵌套的data数组并循环:

foreach(array_column($array['data'], 'data') as $data) {
    echo $data['gender'];
    echo $data['name']['first'];
}

如果schools是可变长度,那么您需要循环或implode(', ', $data['schools'])