Unset one Array on the basis of another array value in php?

138 Views Asked by At

How can I remove one array index on the basis of another array value. For example-

Array1
(
[0] => @@code
[1] => @@label
[2] => @@name
[3] => @@age
)

Array2
(
[0] => 123jj
[1] => test
[2] => john
[3] => 45
)

Array3
(
[0] => 2 #2 is index to be unset in array1 and array2
[1] => 3 #3 is index to be unset in array1 and array2
) 

I have 3 arrays , I want to unset array1 and array2 index on the basis of value of array3 using php.How can I use unset() Method for this?

unset($array1,$array3) #this is wrong, but some thing like that 
unset($array2,$array3) 

With Out for loop.

I should get

Array1
(
[0] => @@code
[1] => @@label
)

Array2
(
[0] => 123jj
[1] => test
)
2

There are 2 best solutions below

2
On

You asked similar question and removed it after getting the answer :

unset array indexs from value of another array?

$firstArray = array( 0 => '@@code' ,1 => '@@label' ,2 => '@@name' ,3 => '@@age' );

$keysArray = array( 0 ,1 );

$resultArray = array_diff_key( $firstArray ,array_flip( $keysArray ) );

var_dump( $resultArray );
1
On

Maybe you need this?

foreach($array3 as $tmp){
  unset($array1[$tmp]);
  unset($array2[$tmp]);
}