1

I have an array structured like so (this is the way the CSV formats it):

Array(

    0 => Array(
        0 => person1
        1 => person2
        2 => person3
        //all the way to 9
    ),

    1 => Array(
        0 => id belonging to person 1
        1 => id belonging to person 2
        2 => id belonging to person 3
    ),

    2 => Array(
        0 => id belonging to person 1
        1 => id belonging to person 2
        2 => id belonging to person 3
    ),

    //all the way to 20
)

I'm trying to sort a new array (of arrays), with each index being the value correspondent to the key in the 0 index above. i.e., person1 points to an array with all ids from the arrays 1-20 outside.

In each of the arrays after the index 0, it contains 20 ids, 0 belongs to the key 0 in the first array.

The structure I'm trying to achieve is shown below:

Array(
    [person1] => Array(
        id belonging to person 1
        id belonging to person 1
        id belonging to person 1
    ),

    [person2] => Array(
        id belonging to person 2
        id belonging to person 2
        id belonging to person 2
    ),

    [person3] => Array(
        id belonging to person 3
        id belonging to person 3
        id belonging to person 3
    ),
)

My attempt so far has worked, however, I had to hard code some of the indexes. What's the best solution to achieve the desired structure?

1 Answer 1

1

I'm a bit unsure if this is what you are looking for...

<?php

$arr = Array(

    0 => Array(
        0 => "person1",
        1 => "person2",
        2 => "person3"
        //all the way to 9
    ),

    1 => Array(
        0 => "id belonging to person 1",
        1 => "id belonging to person 2",
        2 => "id belonging to person 3"
    ),

    2 => Array(
        0 => "id belonging to person 1",
        1 => "id belonging to person 2",
        2 => "id belonging to person 3"
    )
);

foreach($arr[0] AS $id=>$name)
{
    $ids[$id] = $name;
}

foreach(array_slice($arr,1) AS $persons)
{
    foreach($persons AS $id=>$person)
    {
        // make sure to check if $ids[$id] exist and handle it as you like.
        // if(isset($ids[$id]))
        $people[$ids[$id]][] = $person;
    }
}


print_r($people);

?>

result:

Array
(
    [person1] => Array
        (
            [0] => id belonging to person 1
            [1] => id belonging to person 1
        )

    [person2] => Array
        (
            [0] => id belonging to person 2
            [1] => id belonging to person 2
        )

    [person3] => Array
        (
            [0] => id belonging to person 3
            [1] => id belonging to person 3
        )

)

EDIT: Should be noted I'm not making any checks on if the person's id exist in the $ids array, and neither if $people are ever set.

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

4 Comments

@NoahMatisoff Beware what I said in the end, it does not check if the $ids array is when it does $ids[$id] and $people when it does print_r at the end. I also made an edit where I made the two last loops foreach, so their ids doesn't need to be from 0 to n.
Nice refactor. I understand it's prone to having undefined index errors.. But that's not a problem since the data is not dynamic and is coming from a CSV.
@NoahMatisoff I did do some edits to the code around the time of your reply that you may have missed, but I made them all foreach loops, so no bad indexes in the loops. Also, if you want the index for example in the [person1] array to represent the index they came from in $arr, then just do $pid=>$persons in the foreach loop with the slice and do this when assigning to the $people array $people[$ids[$id]][$pid] = $person;. I also commented on where and how you can check for not set indexes in the loops.
I noticed that's why I said nice refactor. Thanks.

Your Answer

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