cpt_wonderful
cpt_wonderful

Reputation: 11

Conditionally add static associative element to row being pushed into a 2d array

I would like to add variable-sized rows to a result array, depending on other values already in the array.

My script:

$today = date("Ymd");
$finalDate = date('Ymd', strtotime('+2 days'));

foreach ($arr as $key => $value) {
    if ($value['DTSTART'] <= $finalDate){
        $values[] = array(
            'day' => $value['DTSTART'],
            'type' => $value['SUMMARY;LANGUAGE=en']
        );
    }
}

Type is something like this: Non-recyclable waste, Bio-waste, etc.

I want to add colors to the array depending on the waste type. I tried it like this, but it adds another 'sub-array'.

foreach ($arr as $key => $value) {
    if ($value['DTSTART'] <= $finalDate){
        $values[] = array(
            'day' => $value['DTSTART'],
            'type' => $value['SUMMARY;LANGUAGE=en']
        );
            
        switch ($value['SUMMARY;LANGUAGE=en']) {
            case "Non-recyclable waste":
                $values[] = array(
                    'color' => 'success'    
                );
                break;
            case "Bio-waste":
                $values[] = array(
                    'color' => 'success'    
                );
                break;
        }
    }
}

The Input array:

Array (
    [0] => Array (
        [DTSTART] => 20210712
        [DTEND] => 20210713
        [TRANSP] => TRANSPARENT
        [LOCATION;LANGUAGE=en] => streetname
        [UID] => uid
        [DTSTAMP] => 20210707T015623Z
        [DESCRIPTION;LANGUAGE=en] => description
        [SUMMARY;LANGUAGE=de] => Bio-waste
        [PRIORITY] => 9
        [CLASS] => PUBLIC [STATUS] => CONFIRMED
    )
    [1] => Array (
        [DTSTART] => 20210726
        [DTEND] => 20210727
        [TRANSP] => TRANSPARENT
        [LOCATION;LANGUAGE=en] => street
        [UID] => uid
        [DTSTAMP] => 20210707T015623Z
        [DESCRIPTION;LANGUAGE=en] => description
        [SUMMARY;LANGUAGE=en] => Non-recyclable waste
        [PRIORITY] => 9
        [CLASS] => PUBLIC
        [STATUS] => CONFIRMED
    )
)

Upvotes: 1

Views: 76

Answers (2)

mickmackusa
mickmackusa

Reputation: 47894

You can spread a conditionally populated array while declaring the associative elements for each pushed row.

Your current process could use in_array() instead of switch(), but for better extendability perhaps match() is a better modern technique.

Code: (Demo)

$arr = [
    ['DTSTART' => '2017-01-02', 'SUMMARY;LANGUAGE=en' => 'Non-recyclable waste'],
    ['DTSTART' => '2017-01-03', 'SUMMARY;LANGUAGE=en' => 'Bio-waste'],
    ['DTSTART' => '2017-01-04', 'SUMMARY;LANGUAGE=en' => 'Junk'],
];
$finalDate = '2038-12-31';

$result = [];
foreach ($arr as $value) {
    if ($value['DTSTART'] > $finalDate) {
        continue;
    }
    
    $result[] = [
        'day' => $value['DTSTART'],
        'type' => $value['SUMMARY;LANGUAGE=en'],
        ...match($value['SUMMARY;LANGUAGE=en']) {
            'Non-recyclable waste', 'Bio-waste' => ['color' => 'success'],
            default => []
        }
    ];
}
var_export($result);

Output:

array (
  0 => 
  array (
    'day' => '2017-01-02',
    'type' => 'Non-recyclable waste',
    'color' => 'success',
  ),
  1 => 
  array (
    'day' => '2017-01-03',
    'type' => 'Bio-waste',
    'color' => 'success',
  ),
  2 => 
  array (
    'day' => '2017-01-04',
    'type' => 'Junk',
  ),
)

Upvotes: 0

Sharky
Sharky

Reputation: 409

Create an item array with unconditional data (day and type). Add conditional data (color) as needed. Push the array to the main array.

$values = [];

foreach ($arr as $key => $value)
{
    if ($value['DTSTART'] <= $finalDate)
    {
        $item = array(
            'day' => $value['DTSTART'],
            'type' => $value['SUMMARY;LANGUAGE=en'],
        );

        switch ($value['SUMMARY;LANGUAGE=en'])
        {
            case "Non-recyclable waste":
            case "Bio-waste":
                $item['color'] = 'success';
            break;
            // Add more cases.
        }
        
        $values[] = $item;
    }
}

Upvotes: 1

Related Questions