Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am working on associative arrays from two days, but somehow could not reach to my answer. I am using for loop for this, let me know if there some better approach to do this.. let suppose I have an associative array like below

 $students    =   array('a' => 84 , 'b' => 92 , 'a' => 93 , 'b' => 47 , 'c' => 73 , 
                        'd' => 59 , 'e' => 91 , 'a' => 78 , 'c' => 85 ,'e' => 68 ,
                        'c' => 84 , 'a' => 100 ,'d' => 92 , 'e' => 85 , 'd' => 83 ,
                        'a' => 92 , 'b' => 68 , 'b' => 79 ,'d' => 79 , 'a' => 84 ,
                        'b' => 89 , 'c' => 69 , 'c' => 67 ,'c' => 92 , 'd' => 73 , 
                        'e' => 79 , 'e' => 84);

and I want this array to convert into a multidimensional array, take all values of 'a' and put this into an array 'a' and apply the same rule for the rest like given below. The out put should be like.

$students = array('a' => array(84, 93, 88, 100, 92, 84), 
                 'b' => array(92, 47, 68, 79, 89),
                 'c' => array(73, 85, 84, 69, 67, 92), 
                 'd' => array(59, 92, 83, 79, 73),
                 'e' => array(91, 68, 85, 79, 84));

How should I do this ? Thanks :)

share|improve this question
3  
That first array is invalid and cannot exist – John Conde Aug 20 at 13:54
3  
You can't have duplicate keys! Do print_r($students); and you will see that you only have 5 elements in your array. – Rizier123 Aug 20 at 13:54
    
Is this possible if I run a loop on that array and collect all the 'a' and push that into an empty array..and do the same for rest ? Is this possible ?? #John, #Rizier – User26 Aug 20 at 13:58
    
No it is not possible if this is written in the source code, – Rizier123 Aug 20 at 13:59
1  
Then how should you do what? You don't have an array like that. You're asking how to transform something that can't exist. – Paul Crovella Aug 20 at 14:10
up vote 0 down vote accepted

Something like this? https://eval.in/626246

<?php

$students = [
    ['a', 84], ['b', 47], ['c', 73],
    ['a', 92], ['a', 93], ['b', 68],
];

$groups = [];
foreach ($students as list($class, $number)) {
    $groups[$class][] = $number;
}
ksort($groups);
foreach ($groups as &$numbers) {
    sort($numbers);
}
unset($numbers); // Do not forget unsetting variable reference

print_r($groups);
share|improve this answer
    
may be... one more thing, I also want to sort the values of 'a','b','c','d','e' . what should I do for this ? – User26 Aug 20 at 14:18
1  
That is a totally new question – RiggsFolly Aug 20 at 14:20
    
and please describe that $students is array or what ? – User26 Aug 20 at 14:35
    
$students is an array. Do not use redundant, old notation array() on PHP 5.4 or later. [] is more simple and clear. – mpyw Aug 20 at 15:25
    
Also list() in foreach assignment is a new feature from PHP 5.5 – mpyw Aug 20 at 15:26

The problem that you do not seem to be grasping is that you do not have an array that looks anything like you are suggesting.

If you show that array after creating it you get this

<?php

$students    =   array('a' => 84 , 'b' => 92 , 'a' => 93 , 'b' => 47 , 'c' => 73 ,
                        'd' => 59 , 'e' => 91 , 'a' => 78 , 'c' => 85 ,'e' => 68 ,
                        'c' => 84 , 'a' => 100 ,'d' => 92 , 'e' => 85 , 'd' => 83 ,
                        'a' => 92 , 'b' => 68 , 'b' => 79 ,'d' => 79 , 'a' => 84 ,
                        'b' => 89 , 'c' => 69 , 'c' => 67 ,'c' => 92 , 'd' => 73 ,
                        'e' => 79 , 'e' => 84);

print_r($students);

This outputs

Array
(
    [a] => 84
    [b] => 89
    [c] => 92
    [d] => 73
    [e] => 84
)

As every time you reuse a key lets say a it overwrites the existing content of the array with a key of a

So basically you do not have the array you think you have and therefore you cannot transform it into anything

share|improve this answer
    
okay... fine :) – User26 Aug 20 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.