如何在PHP中将多维数组过滤为另一个数组?

时间:2019-02-23 15:38:20

标签: php arrays

这个问题可能是重复的,因为我不知道如何描述问题以找到答案。

我有一个包含3个参数的数组:日期,事件和标签。

  • 日期包含Unix时间戳
  • 事件是一种包含事件ID的arrya,
  • 标签是一个包含事件标签的数组(如果数字不是单独的,则用逗号分隔)。

这是数组:

    Array
    (
        [date] => 1554328800
        [events] => Array
            (
                [0] => 130
                [1] => 131
                [2] => 163
            )

        [tags] => Array
            (
                [0] => 4
                [1] => "1,3,4"
                [2] => "1,3"
            )

    )

事件和标签之间的关系在键中,因此位置为0的事件130具有标签4。

您可以看到重复了一些标签(事件130和131或131和163)。

我怎样才能得到只有重复事件的数组:

    Array
    (
      [0] => Array
      (
        [date] => 1554328800
        [events] => Array
            (
                [0] => 130
                [1] => 131
            )

        [tags] => 4
      )
      [1] => Array
      (
        [date] => 1554328800
        [events] => Array
            (
                [0] => 131
                [1] => 163
            )

        [tags] => Array
           (
               [0] => 1
               [1] => 3
           )
      )
    )

3 个答案:

答案 0 :(得分:2)

这是我要怎么做:

  1. 列出每个标签的事件

    这将提供几组事件,可以在下一步中使用

  2. 列出上一步中发生的每组事件的标签

  3. 产生步骤2的结果

以下是代码,也可以在3v4l.org上运行:

// Sample input
$data = [
    "date" => 1554328800,
    "events" => [130, 131, 163],
    "tags" => [4, "1,3,4", "1,3"]
];

// 1. List the events per individual tag
foreach($data["tags"] as $i => $val) {
    $vals = explode(",", $val);
    foreach($vals as $val) {
        $eventsByTag[$val][] = $data["events"][$i];
    }
}

// 2. List the tags per set of events
foreach($eventsByTag as $tag => $events) {
    sort($events, SORT_NUMERIC);
    $tagsByEvents[implode(",", $events)][] = $tag;
}

// 3. produce the result
foreach($tagsByEvents as $events => $tags) {
    $events = explode(",", $events);
    if (count($tags) == 1) $tags = $tags[0];
    $result[] = [
        "date" => $data["date"],
        "events" => $events,
        "tags" => $tags
    ];
}

print_r($result);

答案 1 :(得分:0)

Option Explicit

Sub CallRangeL_Urls()
    Dim iCell As Range
    Dim Sheet1 As Worksheet
    Dim returnValue As String

    Set Sheet1 = ActiveSheet

    For Each iCell In Sheet1.Range("L4:L4")
        ' code here
        Debug.Print "url: "; iCell.Value
        Call ImportData(iCell.Value, returnValue)
        iCell.Offset(0, 1).Value = returnValue

        Debug.Print returnValue
    Next iCell
End Sub

Sub ImportData(urlToOpen As String, ByRef returnValue As String)

'...
'returnValue = Data you want to give back
returnValue = "This is the data we get back from yourUrl: " & urlToOpen & " - DATA/DATA/DATA"  'DataSource...(I didn't read your code again ;-)
End Sub

这不完全符合您的期望,因为它不会合并事件共享标记。我认为这部分还需要开发,否则我可以修改我的答案,以便在必要时为您提供解决方法。

答案 2 :(得分:0)

pl检查此

$sarr=['date'=>1554328800,
    'events'=>
    [
                130,
                131,
                163
    ],
    'tags'=>
    [
        4,
        "1,3,4",
        "1,3"  
    ]

    ]; 
$tagarr=[];
$events=$sarr['events'];
$index=0;
foreach( $sarr['tags'] as $tag)  
{ 
 $t=explode(",",$tag);
 $cnt=count($t);
 for($idx=0;$idx<$cnt;$idx++)
  $tagarr[$t[$idx]][]=$events[$index];
 $index++;
}
$cnt=count($tagarr);
$myarr=[];
foreach($tagarr as $key=>$value)
 {
  $myarr[]=['date'=>$sarr['date'],'events'=>$value,'tags'=>$key];
  }
ec ho "<pre>";
print_r($myarr);
echo "</pre>";

输出是

Array
( 
   [0] => Array
       (
          [date] => 1554328800
          [events] => Array
              (
                  [0] => 130
                  [1] => 131
              )

         [tags] => 4
    )

[1] => Array
    (
        [date] => 1554328800
        [events] => Array
            (
                [0] => 131
                [1] => 163
            )

        [tags] => 1
    )

[2] => Array
    (
        [date] => 1554328800
        [events] => Array
            (
                [0] => 131
                [1] => 163
            )

        [tags] => 3
    )

  )