基于php中的另一个数组值取消设置一个数组?

时间:2013-10-30 14:05:36

标签: php unset array-unset

如何根据另一个数组值删除一个数组索引。 例如 -

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
) 

我有3个数组,我想根据array3的值使用php取消设置array1和array2索引。我可以使用unset()方法吗?

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

使用Out for循环。

我应该

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

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

2 个答案:

答案 0 :(得分:1)

你问了类似的问题并在得到答案后删除了它:

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 :(得分:0)

也许你需要这个?

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