duancongduo4109 2014-02-18 20:35
浏览 21
已采纳

从多个数组中创建一个数组并在某个键中连接唯一值

I need to loop through a bunch of arrays that I am getting back from mysql that contain a lot of duplicate entries and create one array out of several. The arrays will have one or more unique values in them so I need to keep these but some how concatenate them together into one string using commas or semicolons. So far I am not having any luck with this. Here is a phpfiddle

I need it to create a single array like this:

Array
(
    [0] => Array
        (
            [0] => test
            [1] => test
            [2] => test
            [3] => one
            [4] => two
        )

    [1] => Array
        (
            [0] => test1
            [1] => test
            [2] => test
            [3] => three
            [4] => four
        )

    [2] => Array
        (
            [0] => test2
            [1] => test
            [2] => test
            [3] => five, seven
            [4] => six, eight
        )

    [3] => Array
        (
            [0] => test3
            [1] => test
            [2] => test
            [3] => nine
            [4] => ten
        )

)

from an array like this

    Array
(
    [0] => Array
        (
            [0] => test
            [1] => test
            [2] => test
            [3] => one
            [4] => two
        )

    [1] => Array
        (
            [0] => test1
            [1] => test
            [2] => test
            [3] => three
            [4] => four
        )

    [2] => Array
        (
            [0] => test2
            [1] => test
            [2] => test
            [3] => five
            [4] => six
        )

    [3] => Array
        (
            [0] => test2
            [1] => test
            [2] => test
            [3] => seven
            [4] => eight
        )

    [4] => Array
        (
            [0] => test3
            [1] => test
            [2] => test
            [3] => nine
            [4] => ten
        )

)

this is what I am trying:

for($i=0; $i < count($arrayBIG); $i++) {
    if($arrayBIG[$i][0] == $arrayBIG[$i+1][0]) {
        $clean[$i] = array(array_unique(array_merge($arrayBIG[$i],$arrayBIG[$i+1]), SORT_REGULAR));
    }
}
  • 写回答

2条回答 默认 最新

  • dpd7122 2014-02-19 00:12
    关注

    Okay, so I made a new answer based on your updated post. This one was quite a bit more work than the previous version, but here's the rundown: First, I looped through the big array and for each first element, I checked to see if it also appeared in the array multiple times. If it did, I noted down each location in the array so I know which elements to "merge" and then update later.

    After some looping, we can locate the "columns" of data from all of the similar arrays and merge them together by using array_unique and then imploding them into a string.

    Finally, reconstruct the array, unset the original items and insert our new & improved array to the original location.

    // DEFAULT ARRAY
    $arrayBIG = array(
        array('test', 'test', 'test', 'one', 'two'),
        array('test1', 'test', 'test', 'three', 'four'),
        array('test1', 'test', 'test', 'asdfasd', '443llpapos'),
        array('test1', 'test', 'test', '94niwnoowi', 'inoinwoinw'),
        array('test2', 'test', 'test', 'five', 'six'),
        array('test2', 'test', 'test', 'seven', 'eight'),
        array('test3', 'test', 'test', 'nine', 'ten')
    );
    
    
    // STORE THE ITEM OF EACH ARRAY INTO AN ARRAY OF ITS OWN SO WE CAN CHECK FOR DUPES
    foreach ($arrayBIG AS $item_array) {
        $temp_array[] = $item_array[0];
    }
    
    // COUNT THE VALUES OF THE ARRAY AND STORE ANY KEYS THAT APPEAR MORE THAN ONCE
    // THESE WILL BE THE ITEMS WE TRY AND MERGE
    // THIS WILL NOT BE THE NUMERIC KEY, BUT THE TEXT OF THE KEY - EX: 'test2'
    foreach(array_count_values($temp_array) AS $item_count_key => $item_count_val) {
        if ($item_count_val > 1) {
            $dupe_key_array[] = $item_count_key;
        }
    }
    
    
    // LOOP THROUGH THE DUPE KEYS AND FIND THEIR POSITIONS, THEN MERGE THE SIMILAR ITEMS
    foreach ($dupe_key_array AS $dupe_key) {
    
        $dupe_keys = array();
        $new_array = array();
    
    
        // FOR EACH MAIN ARRAY, NOTE THE ACTUAL NUMERIC LOCATION OF THE VALUE IN THE MAIN ARRAY
        foreach ($arrayBIG AS $array_big_key => $array_big_val) {
    
            // WHEN WE FIND A MATCH, ADD THE NUMERIC VALUE TO THE ARRAY
            // THESE WILL BE THE ITEMS THAT WILL BE REPLACED IN THE FINAL ARRAY
            if ($array_big_val[0] == $dupe_key) {
                $dupe_keys[] = $array_big_key;
               }
    
        }
    
        // FOR EACH ITEM, PULL OUT THE "COLUMN" AND MERGE THEM
        for($i = 0; $i < count($array_big_val); $i++) {
    
            $temp_array_1 = array();
    
            // FOR EACH DUPE, GET EACH INDIVIDUAL ITEM FROM EVERY POSITION AND PUT THEM INTO A TEMP ARRAY
            // THIS WILL BE THE "COLUMN" FOR EACH ARRAY.
            foreach ($dupe_keys AS $dupe_keys_val) {
                $temp_array_1[] = $arrayBIG[$dupe_keys_val][$i];
            }
    
            // FILTER OUT DUPES AND THEN IMPLODE IT INTO A COMMA-SEPARATED STRING
            $new_array[] = implode(', ', array_unique($temp_array_1));
    
        }
    
        // UNSET ALL OF THE ITEMS THAT WE ARE GOING TO BE REPLACING            
        foreach ($dupe_keys AS $array_item_to_remove) {
            unset ($arrayBIG[$array_item_to_remove]);
        }
    
    
        // FIND THE FIRST ITEM IN THE ARRAY WE ARE GOING TO REPLACE
        // WE WILL INSERT THE NEW ARRAY AT THIS LOCATION
        $first_array_item_to_replace = array_shift($dupe_keys);
    
        // SPLICE THE MAIN ARRAY AND ADD IN OUR NEW MERGED ARRAY AT THE FIRST POSITION
        array_splice($arrayBIG, $first_array_item_to_replace, 0, array($first_array_item_to_replace => $new_array));
    
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛