japes Sophey
japes Sophey

Reputation: 495

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.

Upvotes: 1

Views: 6045

Answers (4)

Deepak Biswal
Deepak Biswal

Reputation: 4320

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);
}

Upvotes: 1

Cas Bloem
Cas Bloem

Reputation: 5040

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.

Upvotes: 1

David
David

Reputation: 27

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

Upvotes: 0

Serginho
Serginho

Reputation: 7490

I think what you're looking for is array_merge

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

Upvotes: 0

Related Questions