【发布时间】:2015-12-02 10:14:10
【问题描述】:
我是 php 新手,想知道如何使以下操作成为可能...
下面我有几个从数据库行填充的数组。我想有一个新数组 mappingId 连接几行以形成 mappingId 数组的值。下面我使用了 array_combine 但这似乎不起作用。谁能建议使用什么?
$appId = array();
$appDate = array();
$appTime = array();
$appDoctorId = array();
$mappingId = array(); // combined values
for ($i = 0; $i < mysqli_num_rows($resultAppointmentsBooked); $i++)
{
$row = mysqli_fetch_row($resultAppointmentsBooked);
$appId = $row[0];
$ids[] = $row[0];
$appTime[] = $row[3];
$appDate[] = $row[4];
$appDoctorId[] = $row[2];
$mappingId = array_combine($row[4], $row[3], $row[2]);
//Test output
echo "MappingId: $row[4]$row[3]$row[2] <br />";
echo "MappingId2: - $mappingId[$i] <br />";
}
我也通过以下方式使用了'array_merge' ...
$ids[] = $row[0];
$appTime[] = $row[3];
$appDate[] = $row[4];
$appDoctorId[] = $row[2];
$mappingId = array_merge($appDate, $appTime, $appDoctorId);
但是当我打印这些值时...
echo "MappingId2: $mappingId[$i] <br />";
它只输出第一个数组的值。
【问题讨论】:
-
$mappingId应该如何保存数据? -
你得到了什么数据
$resultAppointmentsBooked,你想如何构造$mappingId? -
只是一个挑剔,连接是一个字符串操作,所以你不能将值连接到一个数组中。而array_combine 想要两个数组作为参数(一个用于键,一个用于值),因此传递字符串将不起作用。
-
我想让'MappingId'告诉appDate+appTime+appDoctorId的值。 $resultAppointmentsBooked 本质上是数据库表中的行。 @deepakb @e
-
appDate+appTime+appDoctorId是什么意思?你想要那些带有+符号或类似appDate apTime appDoctorId的值吗?请告诉我。检查我的答案!
标签: php arrays concatenation concat