0
$champions array =

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

This array has always 10 values.

$fbps array =

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

This array has always 1-5 values.

What i want to make

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

My english is bad to explain this, i hope arrays are enough to tell. Sorry for bad title and explanation.

Edit: Ill try to explain it more. For example Shen's key is 0 in first array. $fbps array doesnt have a value named "Shen" so in third array 0 => 0. Lucian's key is 8 in first array. fbps have a value named Lucian. So third arrays 8th key has value "1"

2
  • 1
    i dont saw relation in the end array, it didt have the values from the other two? Commented Mar 1, 2016 at 14:37
  • @ÁlvaroTouzón keys are relation between them. Shen's key is 0 for example. $fbps array doesnt have a value named "Shen" so 0 => 0. Lucian's key is 8 in first array. fbps have a value named Lucian. So third arrays 8th key has value "1" Commented Mar 1, 2016 at 14:40

4 Answers 4

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

Or a more compact version:

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

EDIT:

added in the !== false conditional as matches found in position 0 of the $fbps array incorrectly evaluated to false because 0 also = false in PHP land...

EDIT 2:

This function has O(N) complexity, meaning it'll grow linearly and in direct proportion to the size of the input data set.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you it works! I used compact version. I changed code little bit because it was changing current first array as result instead of making a new one.
That's fine, just make sure you define that new array outside of the foreach loop though.
This is not the best solution. One with array flipping is much faster/better complexity.
Hence the comment about O(N) complexity, and yes, that would be a better solution but consider the fact that the user doesn't know how to loop through an array and derive results from that I figure this solution was much easier to understand right now.
makes sense. note, however, that while your algorithm is of O(N) complexity, searching an array which is not sorted is also (i guess) of O(N). this should give O(N^2).
1

Does the resulting array just have a value of 1 for every element of $fbps that appears in $champions? If so, something like this should do it;

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

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

Comments

1

I know you've already accepted an answer but here is the most efficient solution:

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

If you just want the most compact code and do not care about performance then you can try this:

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

2 Comments

Just wanted to propose replacing values and keys and making it O(1) complexity. Well, not sure how fast array_flip works, but it shd be ~O(1). Well done.
@hummingBird Thanks! I wrote my loop with a new array in mind per OP's comment in the accepted answer: "I changed code little bit because it was changing current first array as result instead of making a new one"
-1

Not as detailed as Garry's answer, but is based on your existing arrays.

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

Edit: I've switched the array_key_exists to 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);

3 Comments

it gave array (size=10) 0 => int 1 1 => int 1 2 => int 1 3 => int 0 4 => int 0 5 => int 0 6 => int 0 7 => int 0 8 => int 0 9 => int 0
That's because Jim's code is wrong, it's using array keys and because your array isn't associative it changes every time, check out my answer instead. The arrays can be whatever you want.
Oops, I meant to use in_array() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.