简体   繁体   中英

how to get a slice of a multidimensional array using an array with index keys

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
                )

        )

)

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

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;
}

You can do it in a more efficient way, Demo

$result = [];
foreach($arr as $v){
    $result[$v["id"]][] = $v;
}
print_r($result);

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