Differences Between: [Versions 400 and 403] [Versions 401 and 403]
1 <?php 2 3 namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; 4 5 use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; 6 use PhpOffice\PhpSpreadsheet\Calculation\Exception; 7 use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; 8 9 class Matrix 10 { 11 use ArrayEnabled; 12 13 /** 14 * Helper function; NOT an implementation of any Excel Function. 15 */ 16 public static function isColumnVector(array $values): bool 17 { 18 return count($values, COUNT_RECURSIVE) === (count($values, COUNT_NORMAL) * 2); 19 } 20 21 /** 22 * Helper function; NOT an implementation of any Excel Function. 23 */ 24 public static function isRowVector(array $values): bool 25 { 26 return count($values, COUNT_RECURSIVE) > 1 && 27 (count($values, COUNT_NORMAL) === 1 || count($values, COUNT_RECURSIVE) === count($values, COUNT_NORMAL)); 28 } 29 30 /** 31 * TRANSPOSE. 32 * 33 * @param array|mixed $matrixData A matrix of values 34 * 35 * @return array 36 */ 37 public static function transpose($matrixData) 38 { 39 $returnMatrix = []; 40 if (!is_array($matrixData)) { 41 $matrixData = [[$matrixData]]; 42 } 43 44 $column = 0; 45 foreach ($matrixData as $matrixRow) { 46 $row = 0; 47 foreach ($matrixRow as $matrixCell) { 48 $returnMatrix[$row][$column] = $matrixCell; 49 ++$row; 50 } 51 ++$column; 52 } 53 54 return $returnMatrix; 55 } 56 57 /** 58 * INDEX. 59 * 60 * Uses an index to choose a value from a reference or array 61 * 62 * Excel Function: 63 * =INDEX(range_array, row_num, [column_num], [area_num]) 64 * 65 * @param mixed $matrix A range of cells or an array constant 66 * @param mixed $rowNum The row in the array or range from which to return a value. 67 * If row_num is omitted, column_num is required. 68 * Or can be an array of values 69 * @param mixed $columnNum The column in the array or range from which to return a value. 70 * If column_num is omitted, row_num is required. 71 * Or can be an array of values 72 * 73 * TODO Provide support for area_num, currently not supported 74 * 75 * @return mixed the value of a specified cell or array of cells 76 * If an array of values is passed as the $rowNum and/or $columnNum arguments, then the returned result 77 * will also be an array with the same dimensions 78 */ 79 public static function index($matrix, $rowNum = 0, $columnNum = null) 80 { 81 if (is_array($rowNum) || is_array($columnNum)) { 82 return self::evaluateArrayArgumentsSubsetFrom([self::class, __FUNCTION__], 1, $matrix, $rowNum, $columnNum); 83 } 84 85 $rowNum = $rowNum ?? 0; 86 $originalColumnNum = $columnNum; 87 $columnNum = $columnNum ?? 0; 88 89 try { 90 $rowNum = LookupRefValidations::validatePositiveInt($rowNum); 91 $columnNum = LookupRefValidations::validatePositiveInt($columnNum); 92 } catch (Exception $e) { 93 return $e->getMessage(); 94 } 95 96 if (!is_array($matrix) || ($rowNum > count($matrix))) { 97 return ExcelError::REF(); 98 } 99 100 $rowKeys = array_keys($matrix); 101 $columnKeys = @array_keys($matrix[$rowKeys[0]]); 102 103 if ($columnNum > count($columnKeys)) { 104 return ExcelError::REF(); 105 } 106 if ($originalColumnNum === null && 1 < count($columnKeys)) { 107 return ExcelError::REF(); 108 } 109 110 if ($columnNum === 0) { 111 return self::extractRowValue($matrix, $rowKeys, $rowNum); 112 } 113 114 $columnNum = $columnKeys[--$columnNum]; 115 if ($rowNum === 0) { 116 return array_map( 117 function ($value) { 118 return [$value]; 119 }, 120 array_column($matrix, $columnNum) 121 ); 122 } 123 $rowNum = $rowKeys[--$rowNum]; 124 125 return $matrix[$rowNum][$columnNum]; 126 } 127 128 /** @return mixed */ 129 private static function extractRowValue(array $matrix, array $rowKeys, int $rowNum) 130 { 131 if ($rowNum === 0) { 132 return $matrix; 133 } 134 135 $rowNum = $rowKeys[--$rowNum]; 136 $row = $matrix[$rowNum]; 137 if (is_array($row)) { 138 return [$rowNum => $row]; 139 } 140 141 return $row; 142 } 143 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body