Php 在现有阵列内创建另一个关联阵列
我试图在数组中创建一个数组,以便将其编码为JSON。我希望我的数组如下所示:Php 在现有阵列内创建另一个关联阵列,php,arrays,associative-array,Php,Arrays,Associative Array,我试图在数组中创建一个数组,以便将其编码为JSON。我希望我的数组如下所示: <pre>Array ( [user] => John Smith Array ( [profile] => [podcast] => Array ( [0] => stdClass Object
<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>
数组
(
[用户]=>约翰·史密斯
排列
(
[个人资料]=>
[podcast]=>阵列
(
[0]=>stdClass对象
(
[id]=>4
[标题]=>如何在阵列中显示阵列?
[说明]=>正在显示意外数组!
[类别]=>1
[持续时间]=>0:05:05
[音频]=>http://demo.web/uploads/podAudio/PodAudio-20190114102432822.mp3
[图片]=>http://demo.web/uploads/podImage/PodImage-20190114102432145.jpg
[添加人]=>1
[更新日期]=>
)
)
)
)
以下是我获取阵列的代码:
<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>
但是,上面的代码为我提供了一个如下所示的数组:
数组
(
[用户]=>阵列
(
[姓名]=>Lenna Smith
[个人资料]=>
[podcast]=>阵列
(
[0]=>stdClass对象
(
[id]=>4
[标题]=>如何在阵列中显示阵列?
[说明]=>正在显示意外数组!
[类别]=>1
[持续时间]=>0:05:05
[音频]=>http://demo.web/uploads/podAudio/PodAudio-20190114102432822.mp3
[图片]=>http://demo.web/uploads/podImage/PodImage-20190114102432145.jpg
[添加人]=>1
[更新日期]=>
)
)
)
)
是否有任何代码需要替换或添加,以便获得所需的数组?如有任何建议,将不胜感激 简单地做以下更改对我有帮助
您的期望输出没有意义-您不能将[user]=>John Smith
作为数组。也许你想用“约翰·史密斯”作为键?这是PHP吗?请始终将语言(和可能的框架)作为标记。我是否必须更改代码以获得所需的输出?如果是这样的话,我需要添加什么以及在哪里?您不能更改为代码以获得该输出,因为您希望的输出具有语法error@DavidWinder,是否可以将“John Smith”设置为键,并在其中具有键和值的关联数组?
<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>
<?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>";
?>