1

I have the data like,

data: [{
      0: {
         id: 1
         name: aaa,
      }
      1: {
         id: 2
         name: bbb,
      }
      3: {
         id: 3
         name: ccc,
      }
      4: {
         id: 2
         name: bbb,
      }
      5: {
         id: 1
         name: aaa,
      }
      6: {
         id: 2
         name: bbb,
      }
}]

Now i want to get the repeated values count and i want my end result be like,

data: [{
      0: {
         id: 1
         name: aaa,
         count: 2
      }
      1: {
         id: 2
         name: bbb,
         count: 3
      }
      2: {
         id: 3
         name: ccc,
         count: 1
      }
    }]

I tried this code,

$loc = []
foreach ($data as $key) {
    if(array_key_exists($key->id,$loc)){
       $loc[$key->id] = $loc[$key->id] + 1;
    }else{
       $loc[$key->id] = 1;
    }
 }

It works but the result is something like,

{
 1: 2
 2: 3
 3: 1
}

But i want to display like i mentioned above. Help me to get the data format right.

3
  • Do you want an object or an array? It's not clear from your symbol usage... Commented Oct 23, 2019 at 5:27
  • I edited my code @Nick, i want the result like i mentioned above Commented Oct 23, 2019 at 5:35
  • Now you just have an array with the same 1 object embedded in it. What Nick was suggesting is that wouldn't your source data be better formatted as an array with multiple elements? Commented Oct 23, 2019 at 5:37

1 Answer 1

1

This code will give you the basic structure you want:

$loc = [];
foreach ($data as $key) {
    if(array_key_exists($key->id,$loc)){
       $loc[$key->id]->count++;
    }else{
       $loc[$key->id] = clone $key;
       $loc[$key->id]->count = 1;
    }
}
print_r($loc);

Output:

Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [name] => aaa
            [count] => 2
        )    
    [2] => stdClass Object
        (
            [id] => 2
            [name] => bbb
            [count] => 3
        )    
    [3] => stdClass Object
        (
            [id] => 3
            [name] => ccc
            [count] => 1
        )    
)

You can then do various manipulations to get something like the JSON you show in your question e.g.

echo json_encode((object)['data' => [(object)array_values($loc)]], JSON_PRETTY_PRINT);

Output:

{
    "data": [
        {
            "0": {
                "id": 1,
                "name": "aaa",
                "count": 2
            },
            "1": {
                "id": 2,
                "name": "bbb",
                "count": 3
            },
            "2": {
                "id": 3,
                "name": "ccc",
                "count": 1
            }
        }
    ]
}

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Nick, It is working great. I was looking for this many hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.