HH\Vector::fromKeysOf
Creates a Vector
from the keys of the specified container
public static function fromKeysOf<Tk as arraykey>(
?KeyedContainer<Tk, mixed> $container,
): Vector<Tk>;
Every key in the provided KeyedContainer
will appear sequentially in the
returned Vector
, with the next available integer key assigned to each.
Parameters
?KeyedContainer<Tk, mixed> $container
- The container with the keys used to create theVector
.
Returns
Vector<Tk>
- AVector
built from the keys of the specified container.
Examples
This example adds string
keys from a Map
to a Vector
as its values:
$fruit_calories = Map {
'apple' => 95,
'orange' => 45,
};
$vegetable_calories = array(
'cabbage' => 176,
'potato' => 163,
);
// Create a Vector from the keys of a Map
$fruit_names = Vector::fromKeysOf($fruit_calories);
var_dump($fruit_names);
// Create a Vector from the keys of an associative array
$vegetable_names = Vector::fromKeysOf($vegetable_calories);
var_dump($vegetable_names);
object(HH\Vector)#2 (2) {
[0]=>
string(5) "apple"
[1]=>
string(6) "orange"
}
object(HH\Vector)#3 (2) {
[0]=>
string(7) "cabbage"
[1]=>
string(6) "potato"
}
This example creates new Vector
s from an int-keyed Map
and an associative array:
$uploaders_by_id = Map {
4993063 => 'Amy Smith',
9361760 => 'John Doe',
};
$commenters_by_id = array(
7424854 => 'Jane Roe',
5740542 => 'Joe Bloggs',
);
// Create a Vector from the integer keys of a Map
$uploader_ids = Vector::fromKeysOf($uploaders_by_id);
var_dump($uploader_ids); // $uploader_ids contains 4993063, 9361760
// Create a Vector from the integer keys of an associative array
$commenter_ids = Vector::fromKeysOf($commenters_by_id);
var_dump($commenter_ids); // $commenter_ids contains 7424854, 5740542
object(HH\Vector)#2 (2) {
[0]=>
int(4993063)
[1]=>
int(9361760)
}
object(HH\Vector)#3 (2) {
[0]=>
int(7424854)
[1]=>
int(5740542)
}