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 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路