0

In short - I have two $ XML and $ DB arrays, both have values like code.

I foreach the first one, I'm downloading the code and looking for it in the second $DB. If $DB contains the code - I'll display the information that exists, if not - it does not. At least I would like it to be - because instead of the information that the key is in the array, I get else

$XML = array(
    array(
        "code" => 456,
    ),
    array(
        "code" => 789,
    ),
);

$DB = array(
    array(
        "code" => 456,
    ),
    array(
        "code" => 789,
    ),
);

foreach ($XML as $product) {


    if (array_key_exists($product['code'], $DB)) {

        echo "Key is in DB array";

    } else {

        echo "Key isn't in DB array";
    }

}
2
  • 6
    Keys in your $DB array are 0 and 1. Commented Sep 12, 2017 at 13:18
  • Yep, as @u_mulder said it is working as intended, you need to look in one of the arrays in $DB or do a nested loop. Commented Sep 12, 2017 at 13:19

2 Answers 2

1

If you are just after the shared codes that are in both arrays, you could use array_intersect and array_column.

<?php

function codes_shared(array $one, array $two) {
    return array_intersect(array_column($one, 'code'), array_column($two, 'code'));
}

Example:

$xml = array(
    array(
        "code" => 456,
    ),
    array(
        "code" => 789,
    ),
);

$one = array(
    array(
        "code" => 789,
    ),
    array(
        "code" => 123,
    ),
);
$two = array(
    array(
        "code" => 456,
    ),
    array(
        "code" => 99,
    ),
);
$three = array(
    array(
        "code" => 33,
    ),
    array(
        "code" => 1,
    ),
);
$four = array(
    array(
        "code" => 456,
    ),
    array(
        "code" => 789,
    ),
);
foreach([$one, $two, $three, $four] as $db) {
    var_dump(codes_shared($xml, $db));
}

Output:

array(1) {
[1]=>
int(789)
}
array(1) {
[0]=>
int(456)
}
array(0) {
}
array(2) {
[0]=>
int(456)
[1]=>
int(789)
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to check value of one array into values of another array.

So you need in_array with array_column like below:-

Explanation:-

1.in_array search a value exist in an array or not.

2.array_column give single-dimensional array based on index-name given to it from a multi-dimensional array.

3.So now code will become like this:- if (in_array(456, Array(0 => 456,1 => 789)){

and now it will work fine.

$db_array = array_column($DB,'code');
foreach ($XML as $product) {
    if (in_array($product['code'],$db_array )) {
        echo "Product code ".$product['code']." is in DB array\n";
    } else {
        echo "Product code ".$product['code']." isn't in DB array\n";
    }
}

Output:-https://eval.in/860191

8 Comments

Why does he need to do like that?
@Tolios do what? The in_array or the method @AlivetoDie has shown?
What Alive wrote in his answer, why is that a (possible) solution to the question ^^ @Script47
I know, but i must wait 8 mins to i can do that
Small optimization, but I'd pull the array_column code out the loop, or rather do it once upfront.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.