I have an array with the following 6 strings.
user1 A
user2 B
user4 C
user2 D
user1 E
I need to create a Dictionary like:
arr['user1'] => ['A', 'E']
arr['user2'] => ['B', 'D']
arr['user4'] => ['C']
How to do this in PHP?
This seems to work...
$arr = array();
foreach($lines as $line) {
list($user, $letter) = explode(" ", $line);
$arr[$user][] = $letter;
}
CodePad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With