0

I have a function that assigns jobs to a technician in a certain area by different time slots. I want to know is there a way to remove the array within the associative array if the array is null? I want to basically take these values and create a csv file to be used, but i have to make sure that I am only using arrays that actually have keys in them.

Here is the code:

public function assignEasternJobs()

    {
        $easternjobs = $this->getEasternJobs();

        $aaronCash = ['installer' => 'Aaron Cash'];

        $wayneforbes = ['installer' => 'Wayne Forbes'];

        $morning = array_filter($easternjobs, function ($e) {
            return $e['RTime'] == '8-10';
        });

        $midday = array_filter($easternjobs, function ($e) {
            return $e['RTime'] == '10-12';
        });


            $i = 0;
            foreach ($morning as $key => $value) {
                if ($i % 2 == 0) {
                    foreach ($aaronCash as $key1 => $val1) {
                        $morning[$key][$key1] = $val1;
                    }
                } else {
                    foreach ($wayneforbes as $key2 => $val2) {
                        $morning[$key][$key2] = $val2;
                    }
                }
                $i++;
            }
        }


        var_export($morning);


        $i = 0;
        foreach ($midday as $key => $value) {
            if ($i % 2 == 0) {
                foreach ($aaronCash as $key1 => $val1) {
                    $midday[$key][$key1] = $val1;
                }
            } else {
                foreach ($wayneforbes as $key2 => $val2) {
                    $midday[$key][$key2] = $val2;
                }
            }
            $i++;
        }

        var_export($midday);

Heres the output:

array (
)array (
  21 =>
  array (
    'JobNumber' => '2',
    'JobType' => '3',
    'Node' => '10',
    'fname' => 'RICARDO',
    'lname' => 'SMITH',
    'RAddress' => 'SUGARAPPLE ST',
    'HomePhone' => '3924651',
    'WorkPhone' => '3276200',
    'RTime' => '10-12',
    'Comment' => 'FROM POLICE STATION, EAST INTO PINEWOOD..3RD LFT ONTO SUGARAPPLE ST;  5TH HSE ON RGT; BEIGE/YELLW #19',
    'FTax' => '1.00',
    'Tag' => '010106',
    'QuotaGroup' => '1.00',
    'Cust_Acct' => '10221401',
    'offernum' => '2136370',
    'installer' => 'Aaron Cash',
  ),
  122 =>
  array (
    'JobNumber' => '30',
    'JobType' => '3',
    'Node' => '213',
    'fname' => 'MONIQUE',
    'lname' => 'SAWYER NAIRN',
    'RAddress' => 'SUTTON ST',
    'HomePhone' => '8017750',
    'WorkPhone' => '2250417',
    'RTime' => '10-12',
    'Comment' => 'TRN ONTO KEMP RD FRM SHIRLEY ST,   1ST LFT BY LODGE BLDG, 2ND RT,     UNPAINTED HSE AT DEAD END. #13     ...SDW',
    'FTax' => '1.00',
    'Tag' => '213308',
    'QuotaGroup' => '1.00',
    'Cust_Acct' => '11390602',
    'offernum' => '2137494',
    'installer' => 'Wayne Forbes',
  ),
  176 =>
  array (
    'JobNumber' => '22',
    'JobType' => '1',
    'Node' => '128',
    'fname' => 'OSMANY',
    'lname' => 'GODEICH',
    'RAddress' => 'HUDSON STREET',
    'HomePhone' => '8148003',
    'WorkPhone' => '',
    'RTime' => '10-12',
    'Comment' => 'VILLAGE RD ONTO ST ANDREWS DR      LFT @ TJUN, 3RD RGT TO 4 WAY JUNT  BLDG ON LFT WHIT/GRN 4PLEX         UNIT#2. lim',
    'FTax' => '1.00',
    'Tag' => '128101',
    'QuotaGroup' => '1.00',
    'Cust_Acct' => '10036246',
    'offernum' => '2137124',
    'installer' => 'Aaron Cash',
  ),
  235 =>
  array (
    'JobNumber' => '5',
    'JobType' => '3',
    'Node' => '27',
    'fname' => 'ROSALIE',
    'lname' => 'BAIN',
    'RAddress' => 'NASSAU VILLAGE',
    'HomePhone' => '4233021',
    'WorkPhone' => '',
    'RTime' => '10-12',
    'Comment' => 'FRM SOLDIER RD TO NASSAU VILLAGE;MKRGT AT T-JUNC;7TH LFT OPP BUDGET,  2ND BUILDING ON THE RIGHT (S&M)    BEIGE/GREEN APT #1 4233021         call b4 arrive',
    'FTax' => '1.00',
    'Tag' => '027401',
    'QuotaGroup' => '1.00',
    'Cust_Acct' => '12621206',
    'offernum' => '2136937',
    'installer' => 'Wayne Forbes',
  ),
)

Please let me know if there is a way this can be done.

Keith Roye
  • 17
  • 1
  • 7
  • You can use `array_filter` with a callback function. – Rishabh Jha Feb 27 '20 at 18:49
  • But I won't know if theres a job actually available for that time zone so the functionality has to be built in and not assumed – Keith Roye Feb 27 '20 at 18:55
  • First, I really hope you didn't just post client and employee personal details publicly... If so, please use mock data for the sake of these people's privacy. As for your issue... if (empty($arr[$key])) unset($arr[$key]); – aowie1 Feb 27 '20 at 19:45
  • its not working. the empty array is still showing – Keith Roye Feb 27 '20 at 19:56
  • Does this answer your question? [Remove empty array elements](https://stackoverflow.com/questions/3654295/remove-empty-array-elements) – TiddlyWin Feb 27 '20 at 21:47

0 Answers0