Jump to content

Arrays


kirk112

Recommended Posts

Hi

 

I hope someone can figure this this out - been stumped for hours and made no progress

 

I have the following array

 

Array

(

    [0] => Array

        (

            [interview_id] => 254

            [job_id] => 466

            [job_title] => DEPARTMENT MANAGER

            [candidate_ref] => 123

            [start_date_for] => 01/06/07

            [interview_date] => 2007-04-26

            [con_1] => con1

            [value_1] => 123

            [con_2] => con2

            [value_2] => 1.11

            [con_3] => con2

            [value_3] => 0

        )

 

    [1] => Array

        (

            [interview_id] => 258

            [job_id] => 542

            [job_title] => SHOWROOM MANAGER

            [candidate_ref] => 123

            [start_date_for] => 02/06/07

            [interview_date] => 2007-04-19

            [con_1] => con1

            [value_1] => 123

            [con_2] =>

            [value_2] => 0

            [con_3] =>

            [value_3] => 0

        )

 

    [2] => Array

        (

            [interview_id] => 259

            [job_id] => 542

            [job_title] => SHOWROOM MANAGER

            [candidate_ref] => 123

            [start_date_for] => 03/06/07

            [interview_date] => 2007-04-19

            [con_1] => con1

            [value_1] => 1000

            [con_2] => con2

            [value_2] => 0

            [con_3] => con3

            [value_3] => 0

        )

 

    [3] => Array

        (

            [interview_id] => 260

            [job_id] => 290

            [job_title] => SALES CONSULTANT

            [candidate_ref] => 123

            [start_date_for] => 04/06/07

            [interview_date] => 2007-04-25

            [con_1] => con1

            [value_1] => 123

            [con_2] => con2

            [value_2] => 123

            [con_3] => con3

            [value_3] => 23

        )

 

)

 

 

What I need to do is loop through the array and if the candidate_ref and job_id match another item in the array then check the start_date_for and remove the entry with the earliest start date.

 

Hope this makes sense

 

 

Link to comment
https://forums.phpfreaks.com/topic/47882-arrays/
Share on other sites

<?php
$startDates = array();
$newArray = array();
foreach ($jobArray as $key=>$job) {
     if (isset($startDates[$job['job_id']])) {
          $startDate = strtotime($startDates[$job['job_id']]['date']);
          $newDate = strtotime($job['start_date_for']);
          if ($startDate > $newDate) $newArray[$job['job_id']] = $jobArray[$startDates[$job['job_id']]['key']];
          elseif ($startDate < $newDate) $newArray[$job['job_id']] = $job;
     } else {
          $startDates[$job['job_id']] = array('key' => $key, 'date' => $job['start_date_for']);
          $newArray[$job['job_id']] = $job;
     }
}
print_r($newArray);
?>

Link to comment
https://forums.phpfreaks.com/topic/47882-arrays/#findComment-233996
Share on other sites

Thank you for your reply,  WOW.

 

It's almost perfect except for I need to add an additional check, I works until you have 1 job_id and 2 different candidate_ref numbers, somehow once it has found a duplicated job_id it need to check the candidate_ref and if they are the same them remove the item for the array with the earliest start date (which it does) but of the candidate_ref's are different then leave the item in the array.

 

thanks for you help!!

Link to comment
https://forums.phpfreaks.com/topic/47882-arrays/#findComment-234017
Share on other sites

<?php
$startDates = $newArray = array();
foreach ($jobArray as $key=>$job) {
     if (isset($startDates[$job['job_id']])) {
          if ($startDates[$job['job_id']]['cref'] != $jobArray[$startDates['job_id']['key']]) continue;
          $startDate = strtotime($startDates[$job['job_id']]['date']);
          $newDate = strtotime($job['start_date_for']);
          if ($startDate > $newDate) $newArray[$job['job_id']] = $jobArray[$startDates[$job['job_id']]['key']];
          elseif ($startDate < $newDate) $newArray[$job['job_id']] = $job;
     } else {
          $startDates[$job['job_id']] = array('key' => $key, 'date' => $job['start_date_for'], 'cref' => $job['candidate_ref']);
          $newArray[$job['job_id']] = $job;
     }
}
print_r($newArray);
?>

Link to comment
https://forums.phpfreaks.com/topic/47882-arrays/#findComment-234737
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.