php - 从数据库中获取数据并创建多维数组
问题描述
我想通过从数据库中获取数据来制作多维数组。但是,创建了由特定用户添加的播客的第二个数组,它没有给出在数据库中处于活动状态的另一个用户的输出。
这是我的代码:
require $_SERVER['DOCUMENT_ROOT'].'/config/init.php';
require CLASS_PATH.'user.php';
require CLASS_PATH.'podcast.php';
$user = new User();
$podcast = new Podcast();
$userList = $user->getAllUserName();
foreach ($userList as $users) {
$fullname = $users->first_name. ' '. $users->last_name;
$data = array(
'name' => $fullname
);
$podcastList = $podcast->getUserPodcast($fullname);
$data['podcast'] = $podcastList;
}
解决方案
您需要建立一个数据列表。将数据创建为 1 项将停止将podcast
数据与fullname
...
$userList = $user->getAllUserName();
$data = [];
foreach ($userList as $users) {
$fullname = $users->first_name. ' '. $users->last_name;
$data[] = array(
'name' => $fullname,
'podcast' => $podcast->getUserPodcast($fullname)
);
}
仅限拥有播客的用户...
$userList = $user->getAllUserName();
$data = [];
foreach ($userList as $users) {
$fullname = $users->first_name. ' '. $users->last_name;
$podcast = $podcast->getUserPodcast($fullname);
if ( !empty($podcast) ) {
$data[] = array(
'name' => $fullname,
'podcast' => $podcast
);
}
}
推荐阅读
- sql - 在 PostgreSQL 中创建“自定义”表
- node.js - 在 Nuxt JS 中设置 Cache-Control 标头适用于 localhost 但不适用于生产
- angular - Angular 8 升级后无法访问嵌套对象
- r - R; 如何选择()包含()字符串的列,其中字符串是列表的任何元素
- c# - ASP.net 核心中 ActionFilterAttribute 的替代方案
- android - 在androidstudio默认工具链`aarch64-linux-android-gcc`不存在
- python - while循环中的Python时间戳表现得很奇怪
- javascript - 使用 javascript 强制刷新浏览器文件缓存
- mysql - 使用 webpack 启动节点应用程序:error cross-env NODE_ENV=development node src
- r - 如何对数据框中的列进行排序,以便一行中的所有相同字符串都在同一列中