Alien
Alien

Reputation: 724

Combine two array in php

I am new in php. I want to combine two array data into one table. My first array is given below:

https://i.sstatic.net/GzeFl.png

My second array is given below :

https://i.sstatic.net/6tpZ0.png

My output will be:

Name Date Start time End time

Billal 1-14-15 11:29 AM 8:30 Pm

Emrul 1-14-15 2:21 PM 8:34 pm

Limon 1-14-15 11:26 AM

Mamun 1-14-15 11:47 AM 8:32 pm

Masum 1-14-15 12:12 PM 8:33 pm

Shahed 1-14-15 11:30 Am

Emrul 1-15-15 11:29 AM 8:30 Pm

Limon 1-15-15 11:47 AM 8:32 pm

Masum 1-15-15 12:12 PM 8:33 pm

Here start time will be 10 am to 2 pm and rest are end time. when start/end time two data found fast data will be counted

Upvotes: 1

Views: 166

Answers (3)

Purinda Gunasekara
Purinda Gunasekara

Reputation: 165

Looks like what you need here is to combine two arrays you have given and order the result based on the time taken.

Here are some best practices to approach your problem.

  • If you ever need to calculate date/time do not use strings, use timestamps because it is just a one big number which you can use to easily manipulate (in your case you just need to subtract start date/time from end date/time to get the total time taken) using simple math.

  • If you store the data in a SQL database then don't retrieve them and calculate the time taken in PHP, instead calculate the durations using SQL before retrieving data. It will massively reduce the application overhead. You also can use SQL sorting to sort your result set.

Please don't attach screenshots of your xdebug output in future, instead paste on to the editor.

Upvotes: 3

JOE LEE
JOE LEE

Reputation: 1058

$array1a = array_chunk($array1, 3);
$array2a = array_chunk($array2, 3);

 $table =array();
foreach($array1a as $v1){
   $tmp = array(
   'name'=>$v1[0],
    'date' => $v1[1],
     'time1' => $v1[2]
    );

  foreach($array2a as $v2){
      if($v2[1] == $v1[1] && $v2[0] == $v1[0]){
        $tmp['time2']=$v2[2];
       }
   }
  $table[] =   $tmp;
}

hope it help

Upvotes: 2

Syam Danda
Syam Danda

Reputation: 587

array_merge() is an useful function for merging two arrays. Here is a simple example.

<?php
 $array1 = array("color" => "red", 2, 4);
 $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
 $result = array_merge($array1, $array2);
 print_r($result);
?>

Upvotes: 0

Related Questions