1

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.

9
  • How should $mappingId hold the data? Commented Dec 2, 2015 at 10:19
  • What data you are getting $resultAppointmentsBooked and how you want to structure $mappingId? Commented Dec 2, 2015 at 10:23
  • Just a nitpick, concatenation is a string operation, so you cannot concatenate values into an array. And array_combine wants two arrays as parameters (one for keys and one for values), so passing strings will not work. Commented Dec 2, 2015 at 10:25
  • I want 'MappingId' to told the value of appDate+appTime+appDoctorId. $resultAppointmentsBooked essentially rows from a database table. @deepakb @e Commented Dec 2, 2015 at 10:38
  • what does appDate+appTime+appDoctorId mean? Do you want those value with a + sign or something like this appDate apTime appDoctorId? Please let me know. Check my answer! Commented Dec 2, 2015 at 10:53

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Array-push seem to do the job!
1

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.

Comments

0

I think what you're looking for is array_merge

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

1 Comment

Please provide a code example to further explain your answer. Also why array_merge.
0

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

1 Comment

Hi there, I've tried this and it results is no output. @david

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.