如何通过仅比较特定键来合并两个阵列?

时间:2013-09-20 19:00:54

标签: php arrays data-structures merge array-merge

所以我有两个数组 - $ twitterData和$ facebookData。

$ twitterData看起来像

[0] => Array
    (
        [text] => Hello World http://somelink
        [type] => twitter
    )

[1] => Array
    (
        [text] => We like to keep our developers happy! 
        [type] => twitter
    )

$ facebookData看起来像

[0] => Array
        (
            [text] => We like to keep our developers happy! http://somelink.com
            [type] => facebook
        )

[1] => Array
        (
            [text] => Take a look
            [type] => facebook
        )

我正在尝试将两个数组合并为一个名为$ socialFeed的数组。问题是我试图以这种方式合并它们,所以让我们说如果“text”键的前50个字符对于任何两个数组项是相同的,那么我将合并的数组只显示一次该项。所以“我们希望让我们的开发人员满意!http://somelink.com”会出现一次(而不是一个有链接,一个没有)。

我尝试过同时使用array_diff和array_intersect,但它们都比较了键的整个值,而不仅仅是文本键的前X个字符。

2 个答案:

答案 0 :(得分:0)

如果条目已存在,则使用您希望共有的值作为数组键,并推送网络:

$feedResult = array();

foreach ($twitterPost AS $entry){
    $newKey = substr($entry["text"], 0 , 10); //first 10chars
    if (isset($feedResult[$newKey])){
       //actually this will never happen, because when iterating the FIRST
       //post array, there will be no entry. But doing so allows to swap arround
       //the processing order of networks.
       $feedResult[$newKey]["network"][] = "twitter"; //add another network
    }else{
       //create new entry
       $feedResult[$newKey] = array("network" => array("twitter"), "text" => $entry["text"]);
    }
}

foreach ($facebookPost AS $entry){
    $newKey = substr($entry["text"], 0 , 10); //first 10chars
    if (isset($feedResult[$newKey])){
       $feedResult[$newKey]["network"][] = "facebook"; //add another network
    }else{
       //create new entry
       $feedResult[$newKey] = array("network" => array("facebook"), "text" => $entry["text"]);
    }
}

最后你得到的东西(未经测试,但想法应该清楚):

["Hello Worl"] => Array
(
  ["Text"] = "Hello World http://somelink"
  ["network"] => Array
  ( 
       [0] => "twitter" 
  )
)
["We like to"] => Array
(
  ["Text"] = "We like to keep our developers happy!"
  ["network"] => Array
  ( 
     [0] => "twitter",
     [1] => "facebook" 
  )
)
["Take a loo"] => Array
(
  ["Text"] = "Take a look"
  ["network"] => Array
  ( 
     [0] => "facebook" 
  )
)

优化是使用COMPLETE文本的md5()哈希作为新密钥。然后它们被合并,当文本值相等时 - (或者哈希值很高,但那是另一个主题,很可能不会发生在新闻帖子中)

答案 1 :(得分:0)

我继续为你们上课。基本上我遍历两个数组并创建一个新数组,只包含来自两个数组的文本数据。在我创建新数组(检查具有相同文本的现有数组数据 - 没有重复)之后,我为每个原始数组设置了布尔变量,并且我遍历唯一文本字符串数组并检查每个数组中的文本字符串。如果我在其中一个原始数组中找到文本,我会更改相应原始数组的布尔值。我测试值的两个布尔变量并相应地设置新的$ socialArr值。希望这是有道理的。代码经过测试并有效。

<?php

class socialArray {

    public function init($twitter,$facebook){
        $this->combinedText = $this->combinedTextArr($twitter,$facebook);
        $this->socialArr = $this->makeSocialArr($twitter,$facebook);
        return $this->socialArr;
    }

    public function combinedTextArr($twitter,$facebook){
        $combinedText = array();
        foreach($twitter as $key => $value){
            if( !in_array($value["text"],$combinedText ) ){
                $combinedText[] = $value["text"];
            }
        }
        foreach($facebook as $key => $value){
            if( !in_array($value["text"],$combinedText ) ){
                $combinedText[] = $value["text"];
            }
        }
        return $combinedText;
    }

    public function makeSocialArr($twitter,$facebook){
        $socialArr = array();
        foreach($this->combinedText as $value){
            $twitterTest = false;
            $facebookTest = false;
            foreach($twitter as $var){
                if( $var["text"] == $value) {
                    $twitterTest = true;
                }
            }
            foreach($facebook as $var){
                if( $var["text"] == $value ) {
                    $facebookTest = true;
                }
            }
            if( $twitterTest === true && $facebookTest === false ) {
                $socialArr[] = array(
                                        'text' => $value,
                                        'type' => 'twitter'
                                    );
            } else if ( $twitterTest === false && $facebookTest === true ) {
                $socialArr[] = array(
                                        'text' => $value,
                                        'type' => 'facebook'
                                    );
            } else if ( $twitterTest === true && $facebookTest === true ) {
                $socialArr[] = array(
                                        'text' => $value,
                                        'type' => 'both'
                                    );
            }
        }
        return $socialArr;
    }

}


$facebook = array();

$facebook[] = array(
                    "text" => "A post on facebook with text and stuff",
                    "type" => "facebook"
                );

$facebook[] = array(
                    "text" => "This occurs in both arrays",
                    "type" => "facebook"
                );

$twitter = array();

$twitter[] = array(
                    "text" => "A tweet of the utmost importance",
                    "type" => "twitter"
                );

$twitter[] = array(
                    "text" => "This occurs in both arrays",
                    "type" => "twitter"
                );

$socArrMaker = new socialArray();
$socialArr = $socArrMaker->init($twitter,$facebook);

echo "<html><head><style type=\"text/css\">body{ font-family: sans-serif; }</style></head><body><pre>\r\n";
print_r($socialArr);
echo "</pre></body></html>\r\n";

产生......

Array
(
    [0] => Array
        (
            [text] => A tweet of the utmost importance
            [type] => twitter
        )

    [1] => Array
        (
            [text] => This occurs in both arrays
            [type] => both
        )

    [2] => Array
        (
            [text] => A post on facebook with text and stuff
            [type] => facebook
        )

)