1

I want to Match Array 1 values with the value of array 2 key DATE and if it found matched value then print value of MIN_TIME

Array 1:

array:13 [
  0 => 2020-02-13
  1 => 2020-02-12
  2 => 2020-02-11
  3 => 2020-02-10
  4 => 2020-02-09
  5 => 2020-02-08
  6 => 2020-02-07
  7 => 2020-02-06
  8 => 2020-02-05
  9 => 2020-02-04
  10 => 2020-02-03
  11 => 2020-02-02
  12 => 2020-02-01
]

Array 2:

array:8 [▼
  0 => {#1192 ▼
    +"USERID": "59"
    +"DATE": "2020-02-13"
    +"MAX_TIME": "17:11:14.0000000"
    +"MIN_TIME": "10:01:55.0000000"
  }
  1 => {#1194 ▼
    +"USERID": "59"
    +"DATE": "2020-02-12"
    +"MAX_TIME": "18:12:20.0000000"
    +"MIN_TIME": "14:08:25.0000000"
  }
  2 => {#1195 ▼
    +"USERID": "59"
    +"DATE": "2020-02-11"
    +"MAX_TIME": "18:22:09.0000000"
    +"MIN_TIME": "15:40:56.0000000"
  }
  3 => {#1196 ▼
    +"USERID": "59"
    +"DATE": "2020-02-10"
    +"MAX_TIME": "16:47:15.0000000"
    +"MIN_TIME": "14:07:19.0000000"
  }
  4 => {#1197 ▼
    +"USERID": "59"
    +"DATE": "2020-02-07"
    +"MAX_TIME": "16:29:40.0000000"
    +"MIN_TIME": "08:39:38.0000000"
  }
  5 => {#1198 ▶}
  6 => {#1199 ▶}
  7 => {#1200 ▶}
]

My Expected output is:

Date    Date From DB    Time
2/13/2020   2/13/2020   10:01:55 AM
2/12/2020   2/12/2020   2:08:25 PM
2/11/2020   2/11/2020   3:40:56 PM
2/10/2020   2/10/2020   2:07:19 PM
2/9/2020        Not Found
2/8/2020        Not Found
2/7/2020    2/7/2020    8:39:38 AM

I searched from google and tried all solution but failed, Can you guys please help me.

2 Answers 2

1

You can use Laravel Collections to achieve this:

$result = collect($array1)
    ->mapWithKeys(function ($item) {
        return [$item => null];
    })
    ->merge(
        collect($array2)->pluck('MIN_TIME', 'DATE')
    )
    ->toArray();

/* Output:
[
    2020-02-13 => 10:01:55.0000000
    2020-02-12 => 14:08:25.0000000
    2020-02-11 => 15:40:56.0000000
    2020-02-10 => 14:07:19.0000000
    2020-02-09 => null
    2020-02-08 => null
    2020-02-07 => 08:39:38.0000000
]
*/

You can use map or transform to get your expected result:

$result = collect($array1)
    ->mapWithKeys(function ($item) {
        return [$item => null];
    })
    ->merge(
        collect($array2)->pluck('MIN_TIME', 'DATE')
    )
    ->map(function ($item, $key) {
        return [
            'Date' => $key,
            'Time' => $item
        ];
    })
    ->toArray();
Sign up to request clarification or add additional context in comments.

Comments

0

In below code we made for loop of first array element which you show in question. And then match with your collection with first where. And print your table Documentation :

https://laravel.com/docs/5.7/collections#method-first-where

$array=[
      0 => 2020-02-13
      1 => 2020-02-12
      2 => 2020-02-11
      3 => 2020-02-10
      4 => 2020-02-09
      5 => 2020-02-08
      6 => 2020-02-07
      7 => 2020-02-06
      8 => 2020-02-05
      9 => 2020-02-04
      10 => 2020-02-03
      11 => 2020-02-02
      12 => 2020-02-01
    ];
    //$dbcollection is the laravel collection which you getting from the db.
    // as per your second array printed we assume it will be same.
    // assign that array in $dbcollection variable.
    echo "<table>";
    foreach($array as $value){
        echo "<tr>";
        echo "<td>{$value}</td>";
        if ($dbrow=$dbcollection->firstWhere('DATE',$value)) {
            echo "<td>{$dbrow->DATE}</td><td>{$dbrow->MIN_TIME}</td>";
        } else {
            echo "<td colspan='2'>Not Found</td>";
        }
        echo "</tr>";
    }
    echo "</table>";

Hope this will be work for you.

Comments

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.