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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在PHP中将多维数组过滤到另一个?相关的知识,希望对你有一定的参考价值。

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

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

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

这是数组:

    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
           )
      )
    )
答案

我将如何做到这一点:

  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);
另一答案
$date = array();
$date['date'] = 1554328800;
$date['events'] = array(130, 131, 163);
$date['tags'] = array(4, "1,3,4", "1,3");

$events_by_tag = array(); //gather events grouped by tag
foreach ($date['events'] as $pos => $event) { //parse all events

    if (is_string($date['tags'][$pos])) { //if tag is a string <=> if there are more than one tag for the current event

        $tags = explode(',', $date['tags'][$pos]); //explode string to loop over the tags
        foreach ($tags as $tag) {
            if (is_array($events_by_tag[$tag])) { //if tag has already been found and then an array exists to store it
                array_push($events_by_tag[$tag], $event);
            } else {
                $events_by_tag[$tag] = array($event); //else create an array for the next times this tag will be found and store it inside
            }
        }

    } else { //if there's a single tag which is a integer

        if (is_array($events_by_tag[$tag])) { //if tag has already been found and then an array exists to store it
            array_push($events_by_tag[$date['tags'][$pos]], $event);
        } else {
            $events_by_tag[$date['tags'][$pos]] = array($event); //else create an array for the next times this tag will be found and store it inside
        }

    }

}

$result_array = array(); //final array reorganized + date
foreach ($events_by_tag as $tag => $events) {
    $tmp_array['date'] = $date['date'];
    $tmp_array['events'] = $events;
    $tmp_array['tags'] = $tag;
    array_push($result_array, $tmp_array);
}

这并不完全符合您的预期,因为它不会合并事件共享标记。我认为这部分需要开发另一篇文章,但除此之外我可以修改我的答案,如果有必要,给你一条路。

另一答案

请检查一下

$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
    )

  )

以上是关于如何在PHP中将多维数组过滤到另一个?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHP 中将多维数组“展平”为简单数组? [复制]

在php中将一维数组转换为多维数组[重复]

在 PHP 中将多维关联数组展平为一维引用数组

在 PHP 中将多维数组转换为 XML 对象 [重复]

通过(例如:标题或描述)值中的关键字过滤多维数组(PHP)

在 PHP 中转置多维数组