Skip to content

Instantly share code, notes, and snippets.

@honzabrecka
Last active August 29, 2015 14:16
Show Gist options
  • Save honzabrecka/85090a06033222ccfcbd to your computer and use it in GitHub Desktop.
Save honzabrecka/85090a06033222ccfcbd to your computer and use it in GitHub Desktop.
Fuck it, I'm PHP!
$a = ['item0', 'item1', 'item2', 'item3', 'item4', 'item5'];
$b = [2, 3];
print_r($a);
$t = array_flip($b);
$r = array_filter($a, function($i) use ($t) {
return !isset($t[$i]);
}, ARRAY_FILTER_USE_KEY);
$s = [];
for ($i = 0, $l = count($a); $i < $l; $i++) {
if (!isset($t[$i])) $s[] = $a[$i];
}
print_r($s);
var_dump($s[2]);
print_r($r);
var_dump($r[2]);
print_r(array_values($r));
<?php
$a = ['a', 'b', 'c', 'd'];
$b = [1, 2];
$r = [];
$j = 0;
for ($i = 0, $j = 0, $al = count($a), $bl = count($b); $i < $al; $i++) {
if ($j < $bl && $i == $b[$j]) {
$j++;
} else {
$r[] = $a[$i];
}
}
print_r($r);
Array
(
[0] => item0
[1] => item1
[2] => item2
[3] => item3
[4] => item4
[5] => item5
)
Array
(
[0] => item0
[1] => item1
[2] => item4
[3] => item5
)
string(5) "item4"
Array
(
[0] => item0
[1] => item1
[4] => item4
[5] => item5
)
<br />
<b>Notice</b>: Undefined offset: 2 in <b>[...][...]</b> on line <b>20</b><br />
NULL
Array
(
[0] => item0
[1] => item1
[2] => item4
[3] => item5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment