Search code examples
phparraysdatabaseicalendar

How to iterate through array with nested arrays to insert into DB (php)


I'm trying to figure out how to take an icalendar file from AirBnB, iterate through the values and then insert those in our MySQL DB.

I'm able to parse the ical file with results using the PHP Class iCalEasyReader with the following results:

Array
    (
     [PRODID;X-RICAL-TZSOURCE=TZINFO] => -//Airbnb Inc//Hosting Calendar 0.8.8//EN
     [CALSCALE] => GREGORIAN
     [VERSION] => 2.0
     [VEVENT] => Array
    (
        [0] => Array
            (
                [DTEND] => Array
                    (
                        [value] => 20210611
                        [type] => DATE
                    )

                [DTSTART] => Array
                    (
                        [value] => 20210610
                        [type] => DATE
                    )

                [UID] => [email protected]
                [SUMMARY] => Airbnb (Not available)
            )

        [1] => Array
            (
                [DTEND] => Array
                    (
                        [value] => 20210711
                        [type] => DATE
                    )

                [DTSTART] => Array
                    (
                        [value] => 20210703
                        [type] => DATE
                    )

                [UID] => [email protected]
                [SUMMARY] => Airbnb (Not available)
            )

        [2] => Array
            (
                [DTEND] => Array
                    (
                        [value] => 20220612
                        [type] => DATE
                    )

                [DTSTART] => Array
                    (
                        [value] => 20220307
                        [type] => DATE
                    )

                [UID] => [email protected]
                [SUMMARY] => Airbnb (Not available)
            )

    )

 )

The only parts of the parsed file needed are the start and end date [DTSTART] and [DTEND] which are a nested array element and the [UID] and [SUMMARY] values which are all within the [VEVENT] array.

My goal is to iterate each array and insert the results into a MySQL DB. I can do the insert no problem and have much of the array parsed but getting the [VALUE] from [DTSTART] and [DTEND] are beyond my current understanding. I've reviewed this PHP Looping post regarding foreach iterating but can't see how to get the values within the nested arrays for the dates.

My code so far is as follows:

<?php
   foreach ($lines[VEVENT] as $events) {
      foreach ($events as $val => $k) {
        echo 'Result: '.$val . ' - '. $k . '<br>';
      }
    }
 ?>

Which results in :

Result: DTSTART - Array
Result: UID - [email protected]
Result: SUMMARY - Airbnb (Not available)
Result: DTEND - Array
Result: DTSTART - Array
Result: UID - [email protected]
Result: SUMMARY - Airbnb (Not available)
Result: DTEND - Array
Result: DTSTART - Array
Result: UID - [email protected]
Result: SUMMARY - Airbnb (Not available)

Which is quite close but not handling the nested array for the date keys or setting the break points to put in the Insert code.

SUMMARY: How to get the date values and create a point in the code to insert the parsed values as a single record and then continue to iterate available data from a parsed iCalendar file?


Solution

  • Based on your current array structure, you need to do

    foreach ($lines[VEVENT] as $events) {
       foreach ($events as $val => $k) {
          if(is_array($k)){ // check $k is array
              echo 'Result: '.$val . ' - '. $k['value'] . '<br>';
              echo 'Result: '.$val . ' - '. $k['type'] . '<br>';
          }else{
             echo 'Result: '.$val . ' - '. $k . '<br>';
          }            
       }
    }
    

    Note: you should replace $val with $k. In general $k refers to key and $val or $v refers to value

    OR (more practical one if you are very confident with your array structure)

    foreach ($lines[VEVENT] as $events) {  
       $DTENDValue = $events['DTEND']['value'];
       $DTENtype = $events['DTEND']['type'];
       $DTSTARTvalue = $events['DTSTART']['value'];
       $DTSTARTtype = $events['DTSTART']['type'];
       $UID = $events['UID'];
       $SUMMARY = $events['SUMMARY'];
    }