获取数组中匹配项的键



我正在尝试解决CSV报告的问题。我试图根据用户选择的内容在报告中找到基于字符串的特定信息。它是一个包含许多不同事件和日期信息的报告(一个事件可以有多个日期,因此每个日期都会重复,导致事件名称多次出现)

用户选择一些表演(特定的事件/日期组合),并使用每个表演的eventId号将它们添加到数组中:

$eventArray = array(232 => 'event one', 21 => 'event four', 443 = 'event one');

每个都是eventid => eventName(可以有重复的事件名称,因为它们在系统中有不同的日期)。

我正在浏览这份报告,内容如下:

       $file_handle = fopen("$reportUrl", "r");
       while(!feof($file_handle))
        {
           $line_of_text = fgetcsv($file_handle, 20000);
           include('logic/finance_logic.php');                                      
        }
       fclose($file_handle);

因此,对于CSV中的每一行,它都运行finance_logic.php文件中的代码。我遇到困难的部分如下:

// CHECK TO SEE IF THE FIRST LINE IS AN EVENT IN THE ARRAY (NOTE, THERE CAN BE DOUPLICATES)
if(in_array(trim(substr($line_of_text[0],6)), $eventArray)) 
    {
        $f = 1;
        // get event name
        $eventName = trim(substr($line_of_text[0],6));
        // date of event
        $eventDateArray = getEventDateArray($eventName);
    }
    if(isset($f))
        {
            $dateSearch = checkDateInArray($line_of_text[0], $eventDateArray);
            if($f == 1 && $dateSearch[0] == 'found')
                {
                    $eventId = $dateSearch[1];
                    $f = 2;
                }

checkDateInArray函数如下:

function checkDateInArray($date, $array)
{
    if(is_array($array))
        {
            foreach($array as $key => $d)
                {
                    if (strpos($date, $d) !== false) 
                        {
                            // return array with first value 'found' and second being the key which is the event id
                            return array('found', $key);
                        }
                }
        }
}

注意:时间并不总是以GMT结束,因为它取决于位置,所以在比较日期时也需要忽略这一点。

所有这些都导致了一个混乱的修复,我确信另一种方法要简单得多。我考虑的一种方式是,创建的事件数组是eventId => eventName,但eventId与数据库中的eventNameeventDate链接。有没有一种方法,我可以创建一个eventArrayeventId => array('eventName', 'eventDate'),然后检查它是否在这些。我不知道该怎么做。另外,CSV布局方式的示例如下:

"Event: Event One"
""
"Performance Date:  15 Feb 2013 07:30 PM GMT"
""
"Venue: Theatre"
""
"Category: English Comedy"
"Discount","A",
"GENERAL ADMISSION",35.00,
"RESERVED",0.00,
"COMPLIMENTARY",0.00,
"SPECIAL",0.00,
"Holds","A","Face Value Amt",
"H: House Seats",16,16,
"Face Value Amt",,,
"--",,,
"Capacity",371,371,
""
"INT"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"GENERAL ADMISSION",119,4165.00,119,4165.00,
"Totals*",119,4165.00,119,4165.00,
""
"PBO"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"*SPECIAL",10,220.00,10,220.00,
"COMPLIMENTARY",4,0.00,4,0.00,
"GENERAL ADMISSION",22,770.00,22,770.00,
"RESERVED",0,0.00,0,0.00,
"Totals*",36,990.00,36,990.00,
""
"PHO"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"*GENERAL ADMISSION",0,0.00,0,0.00,
"*SPECIAL",42,940.00,42,940.00,
"COMPLIMENTARY",10,0.00,10,0.00,
"GENERAL ADMISSION",68,2380.00,68,2380.00,
"RESERVED",0,0.00,0,0.00,
"Totals*",120,3320.00,120,3320.00,
""
"PERFORMANCE TOTALS"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"*GENERAL ADMISSION",0,0.00,0,0.00,
"*SPECIAL",52,1160.00,52,1160.00,
"COMPLIMENTARY",14,0.00,14,0.00,
"GENERAL ADMISSION",209,7315.00,209,7315.00,
"RESERVED",0,0.00,0,0.00,
"Totals*",275,8475.00,275,8475.00,
""
"Abbreviated Performance Totals"
"Event","Performance Date","Discount","Tickets","Face Value Amt",
Event One,15 Feb 2013 07:30 PM GMT,*GENERAL ADMISSION,0,0.00,
Event One,15 Feb 2013 07:30 PM GMT,*SPECIAL,52,1160.00,
Event One,15 Feb 2013 07:30 PM GMT,COMPLIMENTARY,14,0.00,
Event One,15 Feb 2013 07:30 PM GMT,GENERAL ADMISSION,209,7315.00,
Event One,15 Feb 2013 07:30 PM GMT,RESERVED,0,0.00,
,,,275,8475.00,
""
"Event: Event One"
""
"Performance Date:  16 Feb 2013 07:30 PM GMT+04:00"
""
"Venue: Theatre"
""
"Category: English Comedy"
"Discount","A",
"GENERAL ADMISSION",35.00,
"RESERVED",0.00,
"COMPLIMENTARY",0.00,
"SPECIAL",0.00,
""
"INT"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"GENERAL ADMISSION",203,7105.00,203,7105.00,
"Totals*",203,7105.00,203,7105.00,
""
"PBO"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"GENERAL ADMISSION",36,1260.00,36,1260.00,
"RESERVED",0,0.00,0,0.00,
"Totals*",36,1260.00,36,1260.00,
""
"PHO"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"COMPLIMENTARY",15,0.00,15,0.00,
"GENERAL ADMISSION",99,3465.00,99,3465.00,
"RESERVED",-9,0.00,-9,0.00,
"Totals*",105,3465.00,105,3465.00,
""
"PERFORMANCE TOTALS"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"COMPLIMENTARY",15,0.00,15,0.00,
"GENERAL ADMISSION",338,11830.00,338,11830.00,
"RESERVED",-9,0.00,-9,0.00,
"Totals*",344,11830.00,344,11830.00,
""
"Abbreviated Performance Totals"
"Event","Performance Date","Discount","Tickets","Face Value Amt",
Event One,16 Feb 2013 07:30 PM GMT,COMPLIMENTARY,15,0.00,
Event One,16 Feb 2013 07:30 PM GMT,GENERAL ADMISSION,338,11830.00,
Event One,16 Feb 2013 07:30 PM GMT,RESERVED,-9,0.00,
,,,344,11830.00,
""
"Event: Event Four"
""
"Performance Date:  18 Feb 2013 07:30 PM BST"
""
"Venue: Theatre"
""
"Category: English Comedy"
"Discount","A",
"GENERAL ADMISSION",25.00,
"RESERVED",0.00,
"STUDENT",15.00,
"COMPLIMENTARY",0.00,
"Senior Citizen",10.00,
"SPECIAL",0.00,
""
"INT"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"GENERAL ADMISSION",79,1975.00,79,1975.00,
"Totals*",79,1975.00,79,1975.00,
""
"PBO"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"COMPLIMENTARY",2,0.00,2,0.00,
"GENERAL ADMISSION",32,800.00,32,800.00,
"RESERVED",0,0.00,0,0.00,
"Senior Citizen",4,40.00,4,40.00,
"Totals*",38,840.00,38,840.00,
""
"PHO"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"COMPLIMENTARY",10,0.00,10,0.00,
"GENERAL ADMISSION",100,2500.00,100,2500.00,
"RESERVED",0,0.00,0,0.00,
"STUDENT",4,60.00,4,60.00,
"Senior Citizen",2,20.00,2,20.00,
"Totals*",116,2580.00,116,2580.00,
""
"PERFORMANCE TOTALS"
"Discount","A","A Face Value Amt","Tickets","Face Value Amt",
"COMPLIMENTARY",12,0.00,12,0.00,
"GENERAL ADMISSION",211,5275.00,211,5275.00,
"RESERVED",0,0.00,0,0.00,
"STUDENT",4,60.00,4,60.00,
"Senior Citizen",6,60.00,6,60.00,
"Totals*",233,5395.00,233,5395.00,
""
"Abbreviated Performance Totals"
"Event","Performance Date","Discount","Tickets","Face Value Amt",
Event Four,18 Feb 2013 07:30 PM GMT,COMPLIMENTARY,12,0.00,
Event Four,18 Feb 2013 07:30 PM GMT,GENERAL ADMISSION,211,5275.00,
Event Four,18 Feb 2013 07:30 PM GMT,RESERVED,0,0.00,
Event Four,18 Feb 2013 07:30 PM GMT,STUDENT,4,60.00,
Event Four,18 Feb 2013 07:30 PM GMT,Senior Citizen,6,60.00,
,,,233,5395.00,

提前感谢任何能帮忙的人

我是这样做的:

if(!isset($f))
    {
        $eventDateArray = checkIfEventInArray(trim(substr($line_of_text[0],6)), $eventArray);
    }
if($eventDateArray['result'] == true)
    {
        unset($eventDateArray['result']);
        $f = 1;
        // get event name
        $eventName = trim(substr($line_of_text[0],6));
    }
    if(isset($f))
        {
            if($f == 1 && strpos($line_of_text[0], 'Performance Date:') !== false)
                {
                    $dateCheck = checkIfDateInArray($line_of_text[0], $eventDateArray);
                }
            if($f == 1 && $dateCheck['result'] == true)
                {
                    $eventId = $dateCheck['eventId'];
                    $f = 2;
                }
            if($f == 1 && $line_of_text[0] == 'Abbreviated Performance Totals')
                {
                    unset($f);
                }

使用以下函数:

function selectedEventsArray($array)
    {
        if(is_array($array))
            {
                $editedArray = '"' . implode('","', $array) . '"';
                $eventArray = array();
                $sql = mysql_query("SELECT id_event, name_event, date_event, time_event FROM event WHERE id_event IN ($editedArray)");
                while ($row = mysql_fetch_array($sql))
                    {
                        date_default_timezone_set('Europe/Dublin');
                        $date = date_create($row['date_event']);
                        $for_date = date_format($date,'d M Y');
                        $time = date_create($row['time_event']);
                        $for_time = date_format($time,'h:i A');
                        $eventDate = "Performance Date:  $for_date $for_time";
                        $eventArray[$row['id_event']] = array($row['name_event'], $eventDate);
                    }
                return $eventArray;
            }
    }
function checkIfEventInArray($str, $eventArray)
    {
        $newArray = array();
        $newArray['result'] = false;
        foreach ($eventArray as $key => $i)
            {
                if(trim(strtolower(strip_tags($str))) == trim(strtolower(strip_tags($i[0]))))
                    {
                        $newArray['result'] = true;
                        $newArray[$key] = $i[1];
                    }
            }
        return $newArray;
    }
function checkIfDateInArray($dateStr, $eventDateArray)
    {
        $newArray = array();
        $newArray['result'] = false;
        foreach ($eventDateArray as $key => $i)
            {
                if (strpos($dateStr, $i) !== false)
                    {
                        $newArray['result'] = true;
                        $newArray['eventId'] = $key;
                    }
            }
        return $newArray;
    }

相关内容

  • 没有找到相关文章

最新更新