File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,21 @@ public static function firstValue(array $array)
4545 return $ array [$ key ];
4646 }
4747
48+ /**
49+ * @param mixed[] $array
50+ * @param string|int $column
51+ * @return mixed[]
52+ */
53+ public static function columnAsKey (iterable $ array , $ column ): array
54+ {
55+ $ result = [];
56+ foreach ($ array as $ values ) {
57+ $ result [$ values [$ column ]] = $ values ;
58+ }
59+
60+ return $ result ;
61+ }
62+
4863 /**
4964 * @param mixed[] $previous
5065 * @param mixed[] $current
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ use Tester \Assert ;
4+ use Utilitte \Php \Arrays ;
5+
6+ require __DIR__ . '/../bootstrap.php ' ;
7+
8+
9+ Assert::same ([
10+ 5 => ['id ' => 5 ],
11+ 6 => ['id ' => 6 ],
12+ ], Arrays::columnAsKey ([
13+ ['id ' => 5 ], ['id ' => 6 ],
14+ ], 'id ' ));
You can’t perform that action at this time.
0 commit comments