在现有数组内创建另一个关联数组

时间:2019-01-14 09:14:41

标签: php arrays associative-array

我正在尝试在数组内部创建一个数组,以便可以将其编码为JSON。我希望我的数组如下所示:

<pre>Array
(
    [user] => John Smith
              Array
        (
            [profile] => 
            [podcast] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 4
                            [title] => How to display array inside array?
                            [description] => Unexpected array being displayed!
                            [category] => 1
                            [duration] => 0:05:05
                            [audio] => http://demo.web/uploads/podAudio/PodAudio-20190114102432822.mp3
                            [image] => http://demo.web/uploads/podImage/PodImage-20190114102432145.jpg
                            [added_by] => 1
                            [updated_date] => 
                        )

                )

        )

)
</pre>

以下是我获取数组的代码:

<?php  
    $userList = $user->getAllUserName();

    foreach ($userList as $users) { 
        $fullname = $users->full_name;
        $profile = $users->image;
        $id = $users->id;

        $podcastList = $podcast->getUserPodcast($id);

        if (!empty($podcastList)) {
            $output = array(
            'name'  => $fullname,
            'profile'   => $profile,
            'podcast' => $podcastList
        );
        }

    }


    $json = array(
        'user'  => $output
    );
    echo "<pre>";
    print_r($json);
    echo "</pre>";
?>

但是,上面的代码给了我一个看起来像这样的数组:

    <pre>Array
           (
    [user] => Array
        (
            [name] => Lenna Smith
            [profile] => 
            [podcast] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 4
                            [title] => How to display array inside array?
                            [description] => Unexpected array being displayed!
                            [category] => 1
                            [duration] => 0:05:05
                            [audio] => http://demo.web/uploads/podAudio/PodAudio-20190114102432822.mp3
                            [image] => http://demo.web/uploads/podImage/PodImage-20190114102432145.jpg
                            [added_by] => 1
                            [updated_date] => 
                        )

                )

        )

)
</pre>

是否有任何代码需要替换或添加,以便获得所需的数组?任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

只需进行以下更改就对我有所帮助。

<?php 
    $userList = $user->getAllUserName();

    foreach ($userList as $users) { 
        $fullname = $users->full_name;
        $profile = $users->image;
        $id = $users->id;

        $podcastList = $podcast->getUserPodcast($id);

        if (!empty($podcastList)) {
            $output[$fullname] = array(
            'profile'   => $profile,
            'podcast' => $podcastList
        );
        }

    }    

    $json = array(
        'user'  => $output
    );   
     echo "<pre>";
    print_r($json);
    echo "</pre>";
    ?>