public
Created

  • Download Gist
source.php
PHP
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
<?php
 
function num_val(&$n, $keyMode)
{
$ncopy = $n;
if (!is_scalar($n)) {
$ncopy = gettype($ncopy);
/* (string)array() throws a notice on 5.4 */
} elseif ((string)$n === (string)(int)$n) {
$n = (int)$n;
return true;
} elseif ($keyMode && is_string($n)) {
return true;
}
trigger_error("Not a valid "
. ($keyMode ? 'array key' : 'integer') . ": '$ncopy'", E_USER_WARNING);
return false;
}
 
function conv_part($arr, $n, $keyMode)
{
if (!is_array($arr)) {
trigger_error("The depth of the part specification is too large",
E_USER_WARNING);
return false;
}
if (empty($arr)) {
trigger_error("Tried to access part of empty array", E_USER_WARNING);
return false;
}
if (!$keyMode) {
$count = count($arr);
if ($n >= 0) {
if ($n >= $count) {
trigger_error("Index '$n' is too large for the input", E_USER_WARNING);
return false;
}
return $n;
} else {
if (-($n + 1) >= count($arr)) {
trigger_error("Index '$n' is too large in absolute value "
. "for the input", E_USER_WARNING);
return false;
}
return count($arr) + $n;
}
} else {
if ($n === false) {
return 0;
}
if ($n === true) {
return count($arr) - 1;
}
 
$keys = array_keys($arr);
 
$search = array_search($n, $keys, true);
if ($search === false) {
trigger_error("Index '$n' not found in input", E_USER_WARNING);
}
return $search;
}
}
 
function do_unit($read, &$write, &$newReadTargets, &$newWriteTargets,
$index, $nextType)
{
$value = array_values($read)[$index];
 
if ($nextType === 'none') {
$write[] = $value;
} elseif ($nextType === 'multiple') {
$newSubArray = array();
$write[] =& $newSubArray;
 
$newReadTargets[] = $value;
$newWriteTargets[] =& $newSubArray;
} else { //single
$newReadTargets[] = $value;
$newWriteTargets[] =& $write;
}
}
 
function span_is_all($start, $end, $step, $keyMode)
{
return ($step === 1 &&
(($keyMode && $start === false && $end === true)
||
(!$keyMode && $start === 0 && $end === -1)))
||
($step === -1 &&
(($keyMode && $start === true && $end === false)
||
(!$keyMode && $start === -1 && $end === 0)));
}
 
function do_work_span(&$readTargets, &$writeTargets,
$start, $end, $step, $type, $keyMode)
{
$newReadTargets = array();
$newWriteTargets = array();
 
if (($count = count($readTargets)) !== count($writeTargets)) {
trigger_error("Logic error in the algorithm", E_USER_ERROR);
}
 
for ($i = 0; $i < $count; $i++) {
$r = $readTargets[$i];
$w =& $writeTargets[$i];
 
/* As an exception, if we request all elements allow empty arrays */
if ($r === array() && span_is_all($start, $end, $step, $keyMode)) {
continue;
}
 
$s = conv_part($r, $start, $keyMode);
$e = conv_part($r, $end, $keyMode); /* inclusive */
if ($s === false || $e === false)
return false;
 
for ($j = $s;
$step > 0 && $j <= $e || $step < 0 && $j >= $e;
$j += $step)
{
do_unit($r, $w, $newReadTargets, $newWriteTargets, $j, $type);
}
}
 
$readTargets = $newReadTargets;
$writeTargets = $newWriteTargets;
 
return true;
}
 
function do_work_indexes(&$readTargets, &$writeTargets,
$indexes, $type, $keyMode)
{
$newReadTargets = array();
$newWriteTargets = array();
 
if (($count = count($readTargets)) !== count($writeTargets)) {
trigger_error("Logic error in the algorithm", E_USER_ERROR);
}
 
for ($i = 0; $i < $count; $i++) {
$r = $readTargets[$i];
$w =& $writeTargets[$i];
 
foreach ($indexes as $ind) {
$j = conv_part($r, $ind, $keyMode);
if ($j === false)
return false;
 
do_unit($r, $w, $newReadTargets, $newWriteTargets, $j, $type);
}
}
 
$readTargets = $newReadTargets;
$writeTargets = $newWriteTargets;
 
return true;
}
 
function do_work_single_index(&$readTargets, &$writeTargets,
$index, $type, $keyMode)
{
$newReadTargets = array();
$newWriteTargets = array();
if (($count = count($readTargets)) !== count($writeTargets)) {
trigger_error("Logic error in the algorithm", E_USER_ERROR);
}
 
for ($i = 0; $i < $count; $i++) {
$r = $readTargets[$i];
$w =& $writeTargets[$i];
 
$j = conv_part($r, $index, $keyMode);
if ($j === false) {
return false;
}
// var_dump('pre_do_unit', $r, $w);
do_unit($r, $w, $newReadTargets, $newWriteTargets, $j, $type);
}
 
$readTargets = $newReadTargets;
$writeTargets = $newWriteTargets;
 
return true;
}
 
function array_part($arr, array $definition, $keyMode = false)
{
$result = array();
$readTargets = array($arr);
$firstPart = reset($definition);
if (is_array($firstPart)) {
$result[] = array();
$writeTargets = array(&$result[0]);
} else {
$writeTargets = array(&$result);
}
 
for ($depth = 0;
($partSpec = current($definition)) !== false;
$depth++) {
 
$nextPartSpec = next($definition);
if ($nextPartSpec === false)
$nextPartSpec = 'none';
elseif (is_array($nextPartSpec))
$nextPartSpec = 'multiple';
else
$nextPartSpec = 'single';
 
// var_dump($readTargets, $writeTargets, $nextPartSpec);
 
if (is_array($partSpec)) {
if (key_exists('start', $partSpec) ||
key_exists('end', $partSpec)) {
$start = $keyMode ? false /* special value */ : 0;
$end = $keyMode ? true /* special value */ : -1;
$step = 1;
 
foreach ($partSpec as $k => $v) {
if ($v !== null && !num_val($v, $keyMode)) {
return false;
}
if ($k === 'start') {
if ($v === null) {
continue; /* allow null default to 1st element */
}
$start = $v;
} elseif ($k === 'end') {
if ($v === null) {
continue; /* allow null default to last element */
}
$end = $v;
} elseif ($k === 'step') {
if (!num_val($v, false)) {
return false;
}
$step = $v;
if ($step === 0) {
trigger_error("Step cannot be 0", E_USER_WARNING);
return false;
}
} else {
trigger_error("Span definitions should only include "
. "elements with keys 'start', 'end' and 'step', "
. "given '$k", E_USER_WARNING);
return false;
}
}
if ($step < 0) {
/* revert defaults for start and end
* if we're moving backwards */
if ($start === false) {
$start = true;
}
if ($end === true) {
$end = false;
}
}
 
if (!do_work_span($readTargets, $writeTargets,
$start, $end, $step, $nextPartSpec, $keyMode)) {
return false;
}
} else {
$previousKey = -1;
foreach ($partSpec as $k => $v) {
if ($previousKey + 1 !== $k) {
trigger_error("Part definition at depth "
. ($depth + 1) . " is not a sequential array");
}
if (!num_val($v, $keyMode)) {
return false;
}
}
if (!do_work_indexes($readTargets, $writeTargets,
$partSpec, $nextPartSpec, $keyMode)) {
return false;
}
}
} else {
if (!num_val($partSpec, $keyMode)) {
return false;
}
 
if (!do_work_single_index($readTargets, $writeTargets,
$partSpec, $nextPartSpec, $keyMode))
return false;
}
}
 
return $result[0];
}
test.php
PHP
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
<?php
include "source.php";
 
$array = [
[1, 2, 'foo' => 3],
['a', 'foo' => 'b', 'c', 'd'],
];
 
echo 'All, {-1}', "\n";
print_r(
array_part($array, [
['start'=>0, 'end'=>-1],
[-1],
])
);
/*
[
[3],
['d'],
]
*/
 
echo 'All, -1', "\n";
print_r(
array_part($array, [
['start'=>0, 'end'=>-1],
-1,
])
);
/*
[3, 'd']
*/
 
echo "\n", '0, -1', "\n";
print_r(
array_part($array, [
0,
-1,
])
);
/*
3
*/
 
echo "\n", '{0}, -1', "\n";
print_r(
array_part($array, [
[0],
-1,
])
);
/*
[3]
*/
 
echo "\n", 'All, -2;;', "\n";
print_r(
array_part($array, [
['start'=>0, 'end'=>-1],
['start'=>-2, 'end'=>-1],
])
);
/*
[
[2, 3],
['c', 'd']
]
*/
 
echo "\n", 'All, "foo" (KEY MODE)', "\n";
print_r(
array_part($array, [
['start'=>null, 'end'=>null],
"foo",
], true)
);
/*
[3, 'b']
*/
 
echo "\n", 'All, {"foo"->START, -1} (KEY MODE)', "\n";
print_r(
array_part($array, [
['start'=>null, 'end'=>null],
['start' => "foo", 'step' => -1],
], true)
);
/*
[
[3, 2, 1],
['b', 'a'],
]
*/
 
echo "\n", 'Empty array: All, {-1}', "\n";
print_r(
array_part([], [
['start'=>0, 'end'=>-1],
[-1],
])
);
/*
[]
*/
 
echo "\n", '[[1,2],[]]: {-1}, All', "\n";
print_r(
array_part([[1,2], []], [
[-1],
['start'=>0, 'end'=>-1],
])
);
/*
[[]]
*/
 
echo "\n", '[[[1,2]],[]]: All, All, All (KEY MODE)', "\n";
print_r(
array_part([[[1,2]], []], [
['start'=>null],
['start'=>null],
['start'=>null],
], true)
);
/*
original array
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.