-1

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.

ACDC
  • 119
  • 1
  • 7
  • Create an associative array that uses the values from the original array as the keys, and pushes the keys onto an array in the values. Then at the end you can use `array_values()` to convert the associative array into an indexed array. – Barmar Apr 15 '21 at 01:01
  • https://stackoverflow.com/q/43594677/2943403 – mickmackusa Apr 15 '21 at 02:08
  • Whenever you find yourself typing "_StackOverflow is not a free coding service._", this should alert you to the importance of the downvote button and its mouseover text. – mickmackusa Apr 15 '21 at 02:17
  • @Barmar - I put this question around 3 am before I slept and after trying to solve the problem for hours by myself and going through similar questions on StackOverflow. I was very tired and I forgot to put what I tried. I've updated my answer. I hope this explains. – ACDC Apr 15 '21 at 07:33
  • @mickmackusa I tend not to downvote when I'm also voting to close. I see a similar preference expressed in https://meta.stackexchange.com/a/33291/189777 – Barmar Apr 15 '21 at 14:17
  • @Barmar I downvoted the question because there is no close reason for not providing proof of effort. I have tried to garner close votes for unattempted questions, but CodyGray has repeatedly informed me that effort is not a question requirement (I dislike this truth). Even if the OP researched, I expect the question to include a best, failed coding attempt in the [mcve] especially for such a small, basic task. I did not dv the user, I dved the question because I want to discourage future "requirements dump" / "do my work for me" questions. – mickmackusa Apr 15 '21 at 18:29
  • @mickmackusa We have been using "Needs more focus" as that close reason for years. I realize that the text of this close reason doesn't really describe it well, but that's how it is. – Barmar Apr 15 '21 at 18:32
  • @Barmar I know but SOCVR have directly asked me not to request Needs More Focus close votes for very narrow/basic questions that show no effort. They claim that if there are not too many moving parts, then it is not asking too many questions at once. I hunted for an exact dupe - as you know I do - for a good while, but couldn't find one that I was proud of. – mickmackusa Apr 15 '21 at 18:36
  • 1
    @mickmackusa I just upvoted https://meta.stackoverflow.com/questions/274630/should-we-add-a-do-my-work-for-me-close-reason But until something like that is done, we have to make do with the close reasons we have. Questions like this need to be closed, not just downvoted. – Barmar Apr 15 '21 at 18:41

2 Answers2

0

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

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136