简体   繁体   中英

Group values and store indexes of as comma-separated values

I have the following array:

Array
(
    [0] => 1
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 9
    [9] => 9
    [10] => 10
)

In the example above there are various duplicate numbers.

I want the output to be as follows:

 Array
    (
        [0] => 0,1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
        [6] => 7
        [7] => 8,9
        [8] => 10

    )

I would like to be able to create a new array and display the key in the original array as a value in the second one. This means that 0 and 1 share the same value, and 8 and 9 share the same value too.

Update:

I used the following question but I wasn't successful in achieving what I wanted as mine is a flat array. The answers I found only referred to multi-dimensional arrays.

First, you should organize a middle array I called $metaArray :

$array = [1, 1, 2, 3, 4, 5, 6, 7, 9, 9, 10];
$metaArray = array();  
foreach( $array as $key => $value ){
    if (!isset($metaArray[$value])) {
        $metaArray[$value] = $key;
    } else {
        $metaArray[$value] .= ",$key";
    }
}

And you obtain an array like this:

Array
(
    [1] => 0,1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [9] => 8,9
    [10] => 10
)

In $metaArray you've your values as keys and, as values, the keys of the original array associated to that value. Then, if you want to generate the exact output, you should put your elements in your $results :

$result = array();
foreach( $metaArray as $key => $value ){
  array_push($result, (string)$value);    
}

print_r($result);

And you'll obtain the following, where all of your values are strings and not a mix of data types:

Array
(
    [0] => 0,1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8,9
    [8] => 10
)

Alternative solution : If you don't need to have all data as strings, please use the solution suggested by mickmausa to calculate $result , so:

$result = array_values($metaArray);

You need to iterate the loop and check if you are processing the first occurrence of a given value. If so, just push the new keyed element into the result set. If not, use concatenation to append the new value to the old value at the respective key.

Code: ( Demo )

$array = [1, 1, 2, 3, 4, 5, 6, 7, 9, 9, 10];

$result = [];
foreach ($array as $index => $value) {
    if (!isset($result[$value])) {
        $result[$value] = $index;
    } else {
        $result[$value] .= ",$index";
    }
}
var_export(array_values($result));

*I will be happy to withdraw this answer if someone finds a duplicate for this basic task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM