PHP: unset first 3 elements from array -
edit: found alternative this: array_slice() great way.
i have array called $mas
. it's fine me unset last 7 elements of array, when try unset first 3 elements, error:
notice: undefined offset: 0
here few lines of code works:
$ilgis = count($mas); unset($mas[$ilgis-1], $mas[$ilgis-2], $mas[$ilgis-3], $mas[$ilgis-4], $mas[$ilgis-5], $mas[$ilgis-6], $mas[$ilgis-7]);
and code doesn't work:
... unset($mas[0], $mas[1], $mas[2]);
it seems don't exist in array. ideas how fix it?
btw, echo $mas[0];
works perfectly.
var_dump($mas) output:
array (size=9) 0 => string 'paris-orly - stockholm-arlanda' (length=30) 1 => string 'tuesday 25. mar 2014 21:15 - terminal: s' (length=41) 2 => string 'flight dy4314 - lowfare' (length=26) 3 => string 'stockholm-arlanda - copenhagen' (length=30) 4 => string 'wednesday 26. mar 2014 07:00 - terminal: 5' (length=43) 5 => string 'flight dy4151 - lowfare' (length=26) 6 => string '1 adult' (length=7) 7 => string '1 child (2-11)' (length=14) 8 => string '1 infant' (length=8)
instead of unsetting values don't need, select values need. use array_slice()
purpose. advantage of solution on unset()
don't have specify indexes.
$mas = array( 'paris-orly - stockholm-arlanda', 'tuesday 25. mar 2014 21:15 - terminal: s', 'flight dy4314 - lowfare', 'stockholm-arlanda - copenhagen', 'wednesday 26. mar 2014 07:00 - terminal: 5', 'flight dy4151 - lowfare', '1 adult', '1 child (2-11)', '1 infant' ); $output = array_slice($mas, 3); print_r($output);
output:
array ( [0] => stockholm-arlanda - copenhagen [1] => wednesday 26. mar 2014 07:00 - terminal: 5 [2] => flight dy4151 - lowfare [3] => 1 adult [4] => 1 child (2-11) [5] => 1 infant )
Comments
Post a Comment