-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayExtract.php
More file actions
26 lines (22 loc) · 796 Bytes
/
arrayExtract.php
File metadata and controls
26 lines (22 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
include_once 'DataTypes.php';
include_once 'InvalidDataException.php';
/**
* Grabs the indexed data from the table safely
* @param array $array
* @param string|int $index
* @param string $type name of the type to convert to
* @throws InvalidArgumentException if arguments are not of specified type
* @throws InvalidDataException if the index does not exist in the array
* @return
*/
function arrayExtract(array $array, $index, string $type){
if (array_key_exists($index, $array)){
$value = $array[$index];
if (is_null($value))
return $value;
return DataTypes::cast($value, $type);
}
else
throw new InvalidDataException("Specified index '".$index."' does not exist in given array!");
}