0

I have an array

$arr = [
    [
        'id' => 10,
        'name' => 'John',
        'occupation' => 'engineer',
        'points' => 10
    ],
    [
        'id' => 10, 
        'name' => 'John',
        'occupation' => 'librarian',
        'points' => 14
    ],
    [
        'id' => 7,
        'name' => 'Sarah',
        'occupation' => 'artist',
        'points' => 21
    ],
    [
        'id' => 7,
        'name' => 'Sarah',
        'occupation' => 'teacher',
        'points' => 17
    ],  
    [
        'id' => 10,
        'name' => 'John',
        'occupation' => 'butcher',
        'points' => 7
    ],
    [
        'id' => 7,
        'name' => 'Sarah',
        'occupation' => 'engineer',
        'points' => 9
    ],
    [
        'id' => 25,
        'name' => 'Andreea',
        'occupation' => 'judge',
        'points' => 11
    ]
];

And I use this built in functions to get unique ids:

$people = array_column($arr, 'id', 'id');

And then I use a foreach to get every occurrence of each user in the main array $arr:

foreach($people as $id){
    $keys = array_keys(array_column($arr, 'id'), $id);
}

This is the return:

Array
(
    [0] => 0
    [1] => 1
    [2] => 4
)
Array
(
    [0] => 2
    [1] => 3
    [2] => 5
)
Array
(
    [0] => 6
)

Now in order to build a small array for each person I could loop trough this small arrays that contain the keys from the main array and get the values and ending up with small slices.

But, how can I get the actual $arr values for each person instead of getting just the keys? (using as little resources as possible)

I need the result to be like this:

Array
(
    [10] => Array
        (
            [0] => Array
                (
                    [id] => 10
                    [name] => John
                    [occupation] => engineer
                    [points] => 10
                )

            [1] => Array
                (
                    [id] => 10
                    [name] => John
                    [occupation] => librarian
                    [points] => 14
                )

            [2] => Array
                (
                    [id] => 10
                    [name] => John
                    [occupation] => butcher
                    [points] => 7
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [id] => 7
                    [name] => Sarah
                    [occupation] => artist
                    [points] => 21
                )

            [1] => Array
                (
                    [id] => 7
                    [name] => Sarah
                    [occupation] => teacher
                    [points] => 17
                )

            [2] => Array
                (
                    [id] => 7
                    [name] => Sarah
                    [occupation] => engineer
                    [points] => 9
                )

        )

    [25] => Array
        (
            [0] => Array
                (
                    [id] => 25
                    [name] => Andreea
                    [occupation] => judge
                    [points] => 11
                )

        )

)

P.S: I don't need to keep the key index the same.

2
  • What have you tried so far? Where are you stuck? Commented Nov 26, 2019 at 10:53
  • I tried using a second foreach after getting the keys but i think there should be a way to get them all at once when i'm getting the keys. Maybe i shouldn't even get the keys that way? What i'm more interested in is how could i do this with the least amount of resources Commented Nov 26, 2019 at 10:55

2 Answers 2

2

You can do it in a more efficient way,Demo

$result = [];
foreach($arr as $v){
    $result[$v["id"]][] = $v;
}
print_r($result);
Sign up to request clarification or add additional context in comments.

1 Comment

agreed this is the best and efficient way to solve this problem
1

use a function of group by like:

$byGroup = group_by("id", $arr);

function group_by($key, $data) {
    $result = array();

    foreach($array as $val) {
        if(array_key_exists($key, $val)){
            $result[$val[$key]][] = $val;
        }else{
            $result[""][] = $val;
        }
    }

    return $result;
}

3 Comments

Did you test the function? actually you will get an undefined index $array error.....
@Nawin he gave an example in that "like" url.
@HeimEvgi, how efficient is this method?

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.