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

时间:2014-02-18 20:35:56

标签: php arrays

我需要遍历一堆数组,这些数组是我从mysql中获取的,其中包含许多重复的条目,并创建一个数组中的一个数组。数组中将包含一个或多个唯一值,因此我需要保留这些值,但有些是如何使用逗号或分号将它们连接成一个字符串。到目前为止,我对此没有任何好运。这是phpfiddle

我需要它来创建一个像这样的单个数组:

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
        )

)

来自像这样的数组

    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
        )

)

这就是我想要的:

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 个答案:

答案 0 :(得分:1)

我遍历了您的初始代码,并且能够将变量重新分配到临时数组中。我从$ arrayBIG的每个元素中取出了相同的项目:

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

然后,我在这些数组上做了一个array unique并将它们打成一个字符串。最后,我将该字符串添加到最终输出数组中。看起来你在我写这篇文章时已经改变了帖子上的问题代码,但概念应该是相同的。

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

该代码将$new_array输出:

Array
(
    [0] => test
    [1] => test
    [2] => test
    [3] => one, three, five
    [4] => two, four, six
)

答案 1 :(得分:1)

好的,所以我根据你更新的帖子做了一个新答案。这个版本比以前的版本要多得多,但是这里有一个概述:首先,我循环遍历大数组,并且对于每个第一个元素,我检查它是否也多次出现在数组中。如果是这样,我记下了数组中的每个位置,所以我知道哪些元素要“合并”然后再更新。

在一些循环之后,我们可以找到来自所有相似数组的数据的“列”,并使用array_unique将它们合并在一起,然后将它们拼接成一个字符串。

最后,重建数组,取消设置原始项目并插入新的&amp;将阵列改进到原始位置。

// 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));



}