duancongduo4109
duancongduo4109
2014-02-18 20:35

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

已采纳

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 dpd7122 7年前

    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));
    
    
    
    }
    
    点赞 评论 复制链接分享
  • douyan1896 douyan1896 7年前

    I looped through your initial code and was able to just reassign the variables into a temporary array. I pulled out the same item from each element of $arrayBIG:

    $arrayBIG[0][1]
    $arrayBIG[1][1]
    $arrayBIG[2][1] ...
    

    Then, I did an array unique on those arrays and imploded them into a string. Finally, I added that string to the final output array. It looks like you've changed the question's code on the post since I wrote this, but the concepts should still be the same.

    $arrayBIG = array( 0 => array("test", "test", "test", "one", "two"),
                       1 => array("test", "test", "test", "three", "four"),
                       2 => array("test", "test", "test", "five", "six"));
    
    $new_array = array();
    
    for ($j = 0; $j < count($arrayBIG[0]); $j++) {
    
        $temp_array = array();
    
        for ($i = 0; $i < count($arrayBIG); $i++) {
            $temp_array[] = $arrayBIG[$i][$j];
        }
    
        $new_array[] = implode(', ', array_unique($temp_array));
    }
    

    That code will have $new_array outputting this:

    Array
    (
        [0] => test
        [1] => test
        [2] => test
        [3] => one, three, five
        [4] => two, four, six
    )
    
    点赞 评论 复制链接分享