简体   繁体   中英

PHP concatenate values into array

I'm new to php and would like to know how to make the following possible ...

Below I have several arrays populated from Database rows. Id like to have a new array mappingId that concatenates several rows to form the value for the mappingId array. Below I have used array_combine but this doesn't seem to work. Can anyone advise on what to use?

$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 />";
}

I have also used 'array_merge' the following way ...

                $ids[] = $row[0];
                $appTime[] = $row[3];
                $appDate[] = $row[4];
                $appDoctorId[] = $row[2];

                $mappingId = array_merge($appDate, $appTime, $appDoctorId);

but when I print the values ...

             echo "MappingId2: $mappingId[$i] <br />";

It only output the value of the first array.

You can add values to an array by doing this:

 $mappingId[] = 'Hello';
 $mappingId[] = 'There';
 $mappingId[] = 'Friend';

Results in:

 $mappingId = array('Hello', 'There', 'Friend');

--

You can also use array_merge .

This merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

Try something like this. If this is what you need. Here in the below codes $mappingId contains array of individual appDate , appTime , appDoctorId in array format. If you don't want array just want concatenate those value with one element then also you can do that. Please let me know!

$mappingId = array(); // combined values

for ($i = 0; $i < mysqli_num_rows($resultAppointmentsBooked); $i++)
{
    $row = mysqli_fetch_row($resultAppointmentsBooked);
    $appId = $row[0];
    $appTime = $row[3];
    $appDate = $row[4];
    $appDoctorId = $row[2];
    $myArr = array();
    //array_push($myArr, $appDate, $appTime, $appDoctorId);
    // If you want in concatenate format
    array_push($myArr, $appDate."".$appTime."".$appDoctorId);
    array_push($mappingId, $myArr);
}

我认为您正在寻找的是array_merge

array_merge(array(1), array(2,3)); //return array(1,2,3)

try something like this , array_merge should work:

$ids = $row[0];
$appTime = $row[3];
$appDate = $row[4];
$appDoctorId = $row[2];
$mappingId = array_merge($ids, $appTime, $appDate, $appDoctorId);

echo "MappingId: $row[4]$row[3]$row[2] <br />";
echo "MappingId2: - $mappingId <br />";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM