Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Items From an array using PHP

Tags:

arrays

php

I have two arrays

$alpha=array('a','b','c','d','e','f');

$index=array('2','5');

I need to remove the items in the first array that has the index from the second array.

(Remove c-index is 2 and f-index is 5)

So that the returned array is

{'a','b','d','e'}

How can i do this using PHP? Thanks

Edit

Actually I need the final array as follows

[0]=>a
[1]=>b
[2]=>d
[3]=>e

Unsetting will return the array with same indexes

0 => string 'a' 
2 => string 'c' 
3 => string 'd' 
4 => string 'e' 
like image 857
Rav Avatar asked Jul 26 '12 17:07

Rav


2 Answers

foreach ($index as $key) {
    unset($alpha[$key]);
}

had it as array_unset() before.

like image 163
Matt Avatar answered Oct 03 '22 23:10

Matt


Please, try this for more performance:

var_dump(array_diff_key($alpha, array_flip($index)));
like image 37
FabianoLothor Avatar answered Oct 03 '22 23:10

FabianoLothor