Convert existing array into group php [closed]

60
March 05, 2021, at 10:20 AM
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 18 hours ago.

Improve this question

I have any array with some keys, I need to convert into group in PHP

my array :

[
1: {id: "1", callId: "5", memberValue: "25",…}
2: {id: "2", callId: "5", memberValue: "0",…}
3: {id: "3", callId: "5", memberValue: "0",…}
4: {id: "4", callId: "3", memberValue: "0",…}
5: {id: "5", callId: "2", memberValue: "0",…}
]

I need to convert into group by callId like

[
    5: [
        0: {id: "1", callId: "5", memberValue: "25",…}
        1: {id: "2", callId: "5", memberValue: "0",…}
        2: {id: "3", callId: "5", memberValue: "0",…}
    ],
    3: [
        0: {id: "4", callId: "3", memberValue: "0",…}
    ],
    2: [
        0: {id: "5", callId: "2", memberValue: "0",…}
        
    ]   
]

I have tried to check with in_array but it don't work for me.

Answer 1

Just loop it once & your result array will have groups.

    $arrDataSet = [
        [ 'id' => "1", 'callId'=> "5", 'memberValue'=> "25"],
        [ 'id' => "2", 'callId'=> "5", 'memberValue'=> "0"],
        [ 'id'=> "3", 'callId'=> "5", 'memberValue'=> "0"],
        [ 'id'=> "4", 'callId'=> "3", 'memberValue'=> "0"],
        [ 'id'=> "5", 'callId'=> "2", 'memberValue'=> "0"]
    ];
    $resultArray = [];
    foreach( $arrDataSet as $arr ){
       $resultArray[ $arr['callId'] ][] = $arr;
    }
READ ALSO
Accessibility issues with nvda

Accessibility issues with nvda

I have dynamic div which will be added to the HTML page on the flyit's selection dropdown

34
Use multiprocessing sharing memory without pickling large object in Python

Use multiprocessing sharing memory without pickling large object in Python

I did some research, but still don't find an answerIs it possible to create a sharing memory object for multiple processes without pickling the large object?

61
Cannot read enum property, ClassCastException

Cannot read enum property, ClassCastException

For my model I have problem with reading enum propertyAfter reading from mongoDB, the property 'value' has type String instead of MyEnum

78
How to get answers in a random order with handlebars?

How to get answers in a random order with handlebars?

i created a quiz App with NodeJs, storing the questions and answers in MongoDb and using Hanbldebars but i ran into a problemThe code works fine but as you can see the correct answer to the questions appears always in the first place, making the game totally...

60