0

这是我的问题。我们得到了一些不起作用的示例 PHP,所以我一直在尝试或多或少地“靠我的裤子”来写这个。我的正则表达式生锈了,而且我没有处理多维数组,所以这超出了我的经验范围。

我得到一个像这样的数组,但我只想要报告 ID,其中 [1] 包含单词“Export”,然后我需要将它们传递给另一个脚本,我必须或多或少地做同样的事情来获得一个结果集 ID,我可以传递给另一个我可以实际导出的脚本。

 [0] => Array
    (
        [0] => REPORTIDXXXXXXXXXXXXXXXXXXXXXXXXXXX
        [1] => REPORT EXPORT NAME#1
        [2] => REPORT DESCRIPTION #1
        [3] => 2012-10-02T17:31:30
    )

[1] => Array
    (
        [0] => REPORTIDYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
        [1] => REPORTOTHERNAME#2
        [2] => REPORTDESCRIPTION #2
        [3] => 2012-09-28T15:15:17
    )

[2] => Array
    (
        [0] => REPORTIDZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
        [1] => REPORT EXPORT NAME#3
        [2] => REPORT DESCRIPTION #3
        [3] => 2012-09-28T14:59:17
    )
4

1 回答 1

0

像这样的东西应该工作:

function get_report_ids(array $rs) {
   $results = array();
   // loop over your data structure
   foreach($rs as $key => $data) {

      // If element 1 contains EXPORT (case insensitive)
      if(stripos($data[1], 'EXPORT') !== false) {
         // regex to capture the ID from element 0
         if(preg_match('/^EXPORTID(.*)$/i', $data[0], $matches)) {
            // add the ID to the results array
            $results[] = $matches[1];
         }
      }
   }

   // if we had results then return the array, otherwise return null
   return !empty($results) ? $results : null;
}

示例返回值:

array(
  0 => 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
  1 => 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'
)
于 2012-10-02T23:05:06.310 回答