ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
VLookup.php
Go to the documentation of this file.
1<?php
2
4
8
9class VLookup extends LookupBase
10{
24 public static function lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch = true)
25 {
26 $lookupValue = Functions::flattenSingleValue($lookupValue);
27 $indexNumber = Functions::flattenSingleValue($indexNumber);
28 $notExactMatch = ($notExactMatch === null) ? true : Functions::flattenSingleValue($notExactMatch);
29
30 try {
31 $indexNumber = self::validateIndexLookup($lookupArray, $indexNumber);
32 } catch (Exception $e) {
33 return $e->getMessage();
34 }
35
36 $f = array_keys($lookupArray);
37 $firstRow = array_pop($f);
38 if ((!is_array($lookupArray[$firstRow])) || ($indexNumber > count($lookupArray[$firstRow]))) {
39 return Functions::REF();
40 }
41 $columnKeys = array_keys($lookupArray[$firstRow]);
42 $returnColumn = $columnKeys[--$indexNumber];
43 $firstColumn = array_shift($columnKeys);
44
45 if (!$notExactMatch) {
46 uasort($lookupArray, ['self', 'vlookupSort']);
47 }
48
49 $rowNumber = self::vLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch);
50
51 if ($rowNumber !== null) {
52 // return the appropriate value
53 return $lookupArray[$rowNumber][$returnColumn];
54 }
55
56 return Functions::NA();
57 }
58
59 private static function vlookupSort($a, $b)
60 {
61 reset($a);
62 $firstColumn = key($a);
63 $aLower = StringHelper::strToLower($a[$firstColumn]);
64 $bLower = StringHelper::strToLower($b[$firstColumn]);
65
66 if ($aLower == $bLower) {
67 return 0;
68 }
69
70 return ($aLower < $bLower) ? -1 : 1;
71 }
72
73 private static function vLookupSearch($lookupValue, $lookupArray, $column, $notExactMatch)
74 {
75 $lookupLower = StringHelper::strToLower($lookupValue);
76
77 $rowNumber = null;
78 foreach ($lookupArray as $rowKey => $rowData) {
79 $bothNumeric = is_numeric($lookupValue) && is_numeric($rowData[$column]);
80 $bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData[$column]);
81 $cellDataLower = StringHelper::strToLower($rowData[$column]);
82
83 // break if we have passed possible keys
84 if (
85 $notExactMatch &&
86 (($bothNumeric && ($rowData[$column] > $lookupValue)) ||
87 ($bothNotNumeric && ($cellDataLower > $lookupLower)))
88 ) {
89 break;
90 }
91
92 $rowNumber = self::checkMatch(
93 $bothNumeric,
94 $bothNotNumeric,
95 $notExactMatch,
96 $rowKey,
97 $cellDataLower,
98 $lookupLower,
99 $rowNumber
100 );
101 }
102
103 return $rowNumber;
104 }
105}
An exception for terminatinating execution or to throw for unit testing.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static validateIndexLookup($lookup_array, $index_number)
Definition: LookupBase.php:10
static checkMatch(bool $bothNumeric, bool $bothNotNumeric, $notExactMatch, int $rowKey, string $cellDataLower, string $lookupLower, ?int $rowNumber)
Definition: LookupBase.php:25
static vLookupSearch($lookupValue, $lookupArray, $column, $notExactMatch)
Definition: VLookup.php:73
static lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch=true)
VLOOKUP The VLOOKUP function searches for value in the left-most column of lookup_array and returns t...
Definition: VLookup.php:24
static strToLower($pValue)
Convert a UTF-8 encoded string to lower case.