0

我正在调用array_uintersect_assoc()一个回调函数strpbrk()来演示如何根据类型评估不同的返回值。

给定一个关联的干草堆数组,例如:

[
    'one' => '0',
    'two' => '1',
    'three' => '0',
    'four' => '1',
    'five' => 'a',
    'six' => 'b',
    'seven' => 'a',
    'eight' => 'b'
]

还有一个关联的针阵列,例如:

[
    'one' => '1',
    'two' => '0',
    'three' => '0',
    'four' => '1',
    'five' => 'a',
    'six' => 'b',
    'seven' => 'a'
    'eight' => 'b',
]

代码:

var_export(array_uintersect_assoc($array1, $array2, 'strpbrk'));

输出:

['four' => '1']

因为返回的字符串值是真实的ab为什么没有键的元素seveneight包含在结果中?

未专门解决此问题的相关array_uintersect_assoc()页面:

4

1 回答 1

1

PHP 手册声明返回值将被评估为类型int值而不是布尔类型值。

callback(mixed $a, mixed $b): int

此本机函数旨在预期来自三向比较的返回值。这意味着返回零值的元素将被保留,而其他所有元素都将被删除。

在比较seven-keyed 值时, ( strpbrk('a', 'a')) 返回值是字符串值a。(下面的解释将适用于eight-keyed 值的比较。)虽然被认为a是“真实的”是正确的,因为当它被转换为布尔值时,它变成true. true当转换为 int 值时,布尔值将变为也是正确的1。但是,当a直接转换为 int 值时,它就变成了0——这就是array_unintersect_assoc()评估回调的返回值的方式,这解释了为什么元素seveneight没有保留在结果数组中。(此任务的综合演示和以下代码段

var_export([
    // haystack, then needle
    '0:1' => strpbrk('0', '1'), // false
    '1:0' => strpbrk('1', '0'), // false
    '0:0' => strpbrk('0', '0'), // '0'
    '1:1' => strpbrk('1', '1'), // '1'
    'a:b' => strpbrk('a', 'b'), // false
    'b:a' => strpbrk('b', 'a'), // false
    'a:a' => strpbrk('a', 'a'), // 'a'
    'b:b' => strpbrk('b', 'b'), // 'b'
    'a as bool' => (bool)'a', // true
    'a as int' => (int)'a', // 0
    'a as bool then int' => (int)(bool)'a', // 1
]);

因为有这么多演示array_uintersect_assoc()(包括手册)的片段都strcasecmp()用作回调,所以我将提供一些其他回调来为研究人员提供更多上下文。

最终,仅仅理解不同的变量类型是如何被处理为 booleans是不够的。开发人员必须了解不同的值类型如何转换为 int

保留返回零整数的值有点尴尬/不直观——因为零是一个错误的值。对于使用str_contains(), str_starts_with(),str_ends_with()等返回布尔结果的任何人,您需要将本机函数从uintersectto反转,udiff以便true正确处理布尔返回值。

strcmp()

代码:(故障演示

var_export(
    array_uintersect_assoc([
        ['one' => 'a', 'two' => 'aa', 'three' => 'a', 'four' => 'aa'], // string1s
        ['one' => 'aa', 'two' => 'a', 'three' => 'a', 'four' => 'aa'], // string2s
        'strcmp'
    ])
);
// ['three' => 'a', 'four' => 'aa']

修剪()

代码:(故障演示

var_export(
    array_uintersect_assoc([
        ['one' => 'a', 'two' => 'ab', 'three' => '0', 'four' => '1'], // strings
        ['one' => 'ab', 'two' => 'a', 'three' => '1', 'four' => '0'], // masks
        'trim'
    ])
);
// ['one' => 'a', 'two' => 'ab', 'three' => '0']

strcspn()

代码:(故障演示

var_export(
    array_uintersect_assoc([
        ['one' => 'a', 'two' => 'ba', 'three' => 'a', 'four' => 'ba'], // strings
        ['one' => 'ba', 'two' => 'a', 'three' => 'a', 'four' => 'ba'], // masks
        'strcspn'
    ])
);
// ['one' => 'a', 'three' => 'a', 'four' => 'ba']

str_repeat()

代码:(故障演示

var_export(
    array_uintersect_assoc([
        ['one' => 0, 'two' => 1, 'three' => 0, 'four' => 1], // strings
        ['one' => 1, 'two' => 0, 'three' => 0, 'four' => 1], // times
        'str_repeat'
    ])
);
// ['one' => 0, 'two' => 1, 'three' => 0]

str_contains()

代码:(故障演示

var_export(
    array_uintersect_assoc([
        ['one' => 'a', 'two' => 'aa', 'three' => 'a', 'four' => 'aa'], // haystacks
        ['one' => 'aa', 'two' => 'a', 'three' => 'a', 'four' => 'aa'], // needles
        'str_contains'
    ])
);
// ['one' => 'a']
于 2022-01-22T00:03:04.577 回答