数组切片($ data,1,2)我的情况是等价的?

时间:2011-04-12 20:07:06

标签: php arrays

代码:

$data = array("vanilla", "strawberry", "mango", "peaches");
print_r(array_slice($data, 1, 2));


Output:
Array
(
[0] => strawberry
[1] => mango
)

就我而言:

$data = array("vanilla", "strawberry", "mango", "peaches");
$sub_set_data = array( "strawberry", "mango");

输出将是保留数组:      array("vanilla", "peaches");

编辑:

NOT array diff it look like the minus operator 7-3 = 4 or $C = $A-$B

python概念:

>>> A           = [6, 7, 8, 9, 10, 11, 12]
>>> subset_of_A  = [6, 9, 12];
>>> set(A) - set(subset_of_A)
set([8, 10, 11, 7])
>>> 

我如何处理此案?

1 个答案:

答案 0 :(得分:3)

您似乎需要array_diff

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

结果:

Array
(
    [1] => blue
)

http://www.php.net/manual/en/function.array-diff.php