php - Remove child array from array where child item is the last of that item -
my brain broken. here's concept:
$a = array('please', 2); $s = array('help', 3); $d = array('me', 1); $f = array('!', 3); $g = array('asdf', 1); $myarray = array($a, $s, $d, $f, $g); ($i = 0; $i < count($myarray); $i++) { // code here }
code should contain logic removes indexes 0, 3, , 4 myarray because contain last value of kinds (last 2, 3, , 1 respectively) @ position $myarray[$i][1].
--edit: adapted suggestions comments this:
$a = array('please', 2); $s = array('help', 3); $d = array('me', 1); $f = array('!', 3); $g = array('asdf', 1); $myarray = array($a, $s, $d, $f, $g); $used = array(); ($i = 0; $i < count($myarray); $i++) { $used[$myarray[$i][1]] = $i; } foreach ($used $deleteme) unset($myarray[$deleteme]); $newarray = array_values($myarray); var_dump($newarray);
it works, , creating new array array_values eliminates gaps created using unset().
$i = count($myarray); $used = array(); while ($i-- > 0) if (!isset($used[$myarray[$i][1]])) { $used[$myarray[$i][1]] = true; unset($myarray[$i]); }
this simple snippet first iterates on array , deletes index if entry number wasn't deleted yet.
Comments
Post a Comment