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

时间:2016-03-01 14:34:18

标签: php arrays

$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 )

我的英语很难解释这一点,我希望阵列足以说明问题。对不起标题和解释很抱歉。

编辑:我试着解释一下。例如,沉的键在第一个数组中为0。 $ fbps数组没有名为“Shen”的值,因此在第三个数组中0 =>卢西安的关键是第一阵中的8分。 fbps有一个名为Lucian的值。所以第三个数组第8个键的值为“1”

4 个答案:

答案 0 :(得分: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,因为在PHP土地中0也= false ...

编辑2:

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

答案 1 :(得分:1)

对于$ champions中出现的每个$ fbps元素,结果数组的值是否为1?如果是这样的话,应该这样做;

$champions = ['Shen', 'Graves', 'Alister', '...'];
$fbps = ['Shen', 'Alister', '...'];

$result = array_map(function($value) use ($fbps) {
    return (int)in_array($value, $fbps);
}, $champions);

答案 2 :(得分:1)

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

<?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);

答案 3 :(得分:-1)

不像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);