Question

EDIT: I found an alternative to do this: array_slice() is a great way.

I have an array called $mas. It's fine for me to unset last 7 elements of array, but when I try to unset first 3 elements, I get error:

Notice: Undefined offset: 0

Here is few lines of my code that 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 this code doesn't work:

...
unset($mas[0], $mas[1], $mas[2]);

It seems like they even don't exist in this array. Any ideas how to 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)
Was it helpful?

Solution

Instead of unsetting values you don't need, you could select the values you need. Use array_slice() for this purpose. The advantage of this solution over unset() is that you don't have to specify the 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
)

Demo

OTHER TIPS

Or you can use array_shift, wich take the first element and remove it from the array, doesn't matter wich key in case of associative arrays :

array_shift($mas) ;
array_shift($mas) ;
array_shift($mas) ;

unset() with the first 3 index works fine for me:

$mas = array(
  0 => 'Paris-Orly - Stockholm-Arlanda', 
  1 => 'Tuesday 25. Mar 2014 21:15 - Terminal: S',
  2 => 'Flight DY4314 - LowFare' ,
  3 => 'Stockholm-Arlanda - Copenhagen',
  4 => 'Wednesday 26. Mar 2014 07:00 - Terminal: 5',
  5 => 'Flight DY4151 - LowFare' ,
  6 => '1 Adult' ,
  7 => '1 Child (2-11)' ,
  8 => '1 Infant' );
unset($mas[0], $mas[1], $mas[2]);

var_dump(array_values($mas));

Demo

It however preserves the keys, so might be you are trying to access the index 0 after unsetting. This will throw you the undefined offset error and you need to re-index your array. The example above uses array_values() to do the same.

It is quite possible you first three elements to have keys that are not numeric.

Indices 0,1 and 2 may not even exist.

Check this code out:

for ( $i = 0 ; $i < 3 ; $i++ ) {
    foreach ( $mas AS $key => $value ) {
        unset( $mas[$key] );
        break;
    }
    reset($mas);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top