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

用户10810388

我正在尝试在数组中创建一个数组,以便可以将其编码为 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>

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

用户10810388

简单地进行以下更改对我有帮助。

<?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>";
    ?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

检查数组的所有项目是否在另一个关联数组中可用?

使用另一个数组作为键创建关联数组

将关联数组添加到PHP中的另一个关联数组

根据另一个关联数组制作关联数组

基于另一个关联数组制作关联数组

如何基于另一个数组中的条件从关联数组中提取?

从另一个数组返回PHP中的关联数组的函数

另一个对象的值作为Javascript中另一个关联数组中的值

解析关联数组以根据另一个索引的值显示所有值

将一个关联数组转换为一个关联数组,该关联数组具有一个键,该键中的另一个关联数组作为键在php中的值

在Bash中访问JSON对象-关联数组/列表/另一个模型

使用一个关联数组中的值来测试另一个关联数组中的值

在Rust中创建一个关联数组

在jQuery中创建一个关联数组

一个衬垫,用于在关联数组中动态创建键,或者,如果存在,将值推送到现有键的数组

没有数组的另一个数组中的Android JSON解析数组

如何通过继续增加键将关联添加到另一个关联数组中?

如何将关联数组附加到另一个数组的末尾?

从现有数组中创建一个新数组

删除数组中另一个数组中的所有数字

显示来自一个关联数组的值,该数组的键作为另一个数组中的值存在

如果现有数组中列出了另一个属性,如何在Mongoose中投影一个新的布尔字段?

创建一个具有多个值的关联数组到php中的一个键

使用一个多维关联数组对另一个多维数组进行计数或求和

bash:将关联数组作为参数传递给另一个脚本

声明关联数组时尝试访问另一个字段

Shell 脚本:将关联数组传递给另一个 Shell 脚本

在TypeScript中将数组项推入另一个数组的现有对象中

从2个输入创建一个关联数组