如何使用其他 2 个数组制作数组


How to make an array using other 2 array

$champions array =
Array ( 
[0] => Shen
[1] => Graves 
[2] => Lux 
[3] => Tristana 
[4] => Janna 
[5] => Lissandra 
[6] => RekSai 
[7] => Anivia 
[8] => Lucian 
[9] => Alistar ) 

此数组始终具有 10 个值。

$fbps array =
Array (
[0] => RekSai 
[1] => Alistar 
[2] => Lucian ) 

此数组始终具有 1-5 个值。

我想做什么

Array (
[0] => 0 
[1] => 0 
[2] => 0 
[3] => 0 
[4] => 0 
[5] => 0 
[6] => 1 
[7] => 0 
[8] => 1 
[9] => 1 )

我的英语不好解释这一点,我希望数组足以说明问题。很抱歉标题和解释不好。

编辑:我试图解释更多。例如,Shen 的键在第一个数组中是 0。$fbps数组没有名为"Shen"的值,因此在第三个数组 0 => 0 中。路西恩的密钥在第一个数组中是 8。FBP 的值名为 Lucian。所以第三个数组第 8 个键的值为"1"

$cArr = array('Shen','Graves','Lux','Tristana','Janna','Lissandra','RekSai','Anivia','Lucian','Alistar');
$fbps = array('RekSai','Anivia','Lucian');
foreach ($cArr as $key=>$value) {
    if(array_search($value, $fbps) !== false) {
        $cArr[$key] = 1;
    } else {
        $cArr[$key] = 0;
    }
}
var_dump($cArr);

或者更紧凑的版本:

$cArr = array('Shen','Graves','Lux','Tristana','Janna','Lissandra','RekSai','Anivia','Lucian','Alistar');
$fbps = array('RekSai','Anivia','Lucian');
foreach ($cArr as $key=>$value) {
    $cArr[$key] = (array_search($value, $fbps) !== false) ? 1 : 0;
}
var_dump($cArr);

编辑:

在 !== false 条件中添加,因为在 $fbps数组的位置 0 中找到的匹配项错误地计算为 false,因为 0 在 PHP 土地上也 = false...

编辑2:

此函数具有 O(N) 复杂性,这意味着它将线性增长,并且与输入数据集的大小成正比。

生成的数组对于$champions中出现的每个$fbps元素是否只有值 1?如果是这样,这样的事情应该可以做到;

$champions = ['Shen', 'Graves', 'Alister', '...'];
$fbps = ['Shen', 'Alister', '...'];
$result = array_map(function($value) use ($fbps) {
    return (int)in_array($value, $fbps);
}, $champions);
我知道

你已经接受了答案,但这是最有效的解决方案:

<?php
// Your arrays
$champions = array('Shen','Graves','Lux','Tristana','Janna','Lissandra','RekSai','Anivia','Lucian','Alistar');
$fbps = array('RekSai','Alistar','Lucian');
// New array which will store the difference
$champ_compare = array();
// Flip the array so that it is associative and uses the names as keys
// http://php.net/manual/en/function.array-flip.php
$fbps = array_flip($fbps);
// Loop all champions and use $v as reference
foreach($champions as &$v)
{
    // Check for the existent of $v in the associative $fbps array
    // This is leaps and bounds faster than using in_array()
    // Especially if you are running this many times with an unknown number of array elements
    $champ_compare[] = (int)isset($fbps[$v]);
}
unset($v);
// Flip it back if you need to
$fbps = array_flip($fbps);
print_r($champ_compare);

如果您只想要最紧凑的代码而不关心性能,那么您可以尝试以下操作:

<?php
// Your arrays
$champions = array('Shen','Graves','Lux','Tristana','Janna','Lissandra','RekSai','Anivia','Lucian','Alistar');
$fbps = array('RekSai','Alistar','Lucian');
// New array which will store the difference
$champ_compare = array();
// Loop all champions and use $v as reference
foreach($champions as &$v)
{
    // Check if the current champion exists in $fbps
    $champ_compare[] = (int)in_array($v, $fbps);
}
unset($v);
print_r($champ_compare);

不像 Garry 的答案那么详细,但基于您现有的数组。

$res = array()
foreach($champions as $key => $val) {
    $res[$key] = (in_array($val, $fbps)) ? 1 : 0;
}
var_dump($res)

编辑:我已将array_key_exists切换到in_array。

<?php
$champions = array('Shen', 'Graves', 'Lux', 'Tristana', 'Janna', 'Lissandra', 'RekSai', 'Anivia', 'Lucian', 'Alistar');
$fbps = array('RekSai', 'Alistar', 'Lucian');
$res = array();
foreach($champions as $key => $val) {
    $res[$key] = (in_array($val, $fbps)) ? 1 : 0;
}
var_dump($res);