Check the existence of the values of an array in the keys of another

1

I have two arrays:

$ keys

Array
(
    [0] => Array
        (
            [0] => {3}
            [1] => {1}
            [2] => {2}
            [3] => {0}
            [4] => {4}
            [5] => {5}
        )

);

$ properties

Array
(
    [0] => Array
        (
            [0] => var::terms::
            [1] => var::create_terms >> 1::
            [2] => const::EXAMPLE123::
            [3] => var::create_terms>>0::
            [4] => text::serio mesmo::
            [5] => ::ENTER::
        )

    ... // outras keys que não estão relacionadas

)

And I would like to know which way is most correct and recommended in terms of perfomance and semantics, check the existence of the values of array 1 (without% with brackets%) in the keys of array 2 ( I have two examples )

$keys[1] = array_map(function ($keys) {
    return str_replace(['{', '}'], null, $keys);
}, $keys[0]);
sort($keys[1]);

if (in_array($keys[1], [array_keys($properties[0])], false)) {...} else {...}

OR

$keys[1] = array_map(function ($keys) {
    return str_replace(['{', '}'], null, $keys);
}, $keys[0]);

foreach ($keys[1] as $key) {
    if (array_key_exists($key, $properties[0])) {...} else {...}
}
    
asked by anonymous 12.07.2016 / 18:25

3 answers

2

You can use the array_intersect() function to know which values the arrays have in common, array_flip() is used to invert the key / value pair.

<?php
$arr1[0] = [10,28,99];
$arr2[0] = ['var::terms::', 10 => ' var::create_terms >> 1::' , 99 => 'const::EXAMPLE123::', 28 =>'var::create_terms>>0::'];

$novo = array_intersect($arr1[0], array_flip($arr2[0]));

Output:

Array
(
    [0] => 10
    [1] => 28
    [2] => 99
)

Or mount the reverse process:

<?php
$arr1[0] = [10,28];
$arr2[0] = ['var::terms::', 10 => ' var::create_terms >> 1::' , 99 => 'const::EXAMPLE123::', 28 =>'var::create_terms>>0::'];
$novo = array_intersect_key($arr2[0], array_flip($arr1[0]));

Output:

Array
(
    [10] =>  var::create_terms >> 1::
    [28] => var::create_terms>>0::
)
    
12.07.2016 / 19:27
1

You can do just with a foreach, and go see the keys there are one by one:

$hey1 = array(
    0 => array(
        0 => '{2}',
        1 => '{3}',
        2 => '{0}',
        3 => '{2}'
    )
);

$hey2 = array(
    0 => array(
        0 => 'dwe',
        1 => 'dwed',
        2 => 'dsa'
    ),
);

foreach($hey1[0] as $key1 => $value1) {
    $key2 = str_replace(['{', '}'], '', $value1);
    if(in_array($key2, array_keys($hey2[0]))) {
        echo $key1. ' => ' .$value1. ' existe: (' .$key2. ' => ' .$hey2[0][$key2]. ')<br>';
        continue;
    }
    echo $key1. ' => ' .$value1. ' não existe<br>';
}
    
12.07.2016 / 19:05
1

I think I would do something simple like this:

$keys = array(
    0 => array(
        '{3}',
        '{1}',
        '{2}',
        '{5}'
    ),
);

$arr = array(
    0 => array(
        0 => 'foo 0',
        1 => 'foo 1',
        2 => 'foo 2',
        3 => 'foo 3'
    ),
);

$rs = array();
foreach ($keys[0] as $v) {
    $k = (int)$v;
    if (isset($arr[0][$k])) {
        $rs[] = $k;
    }
}

if (!empty($rs)) {
    print_r($rs);
} else {
    echo 'nothing';
}

I did not try different techniques to tell if it is more performative than an X or Y technique. I prefer to use simple things, where even an inexperienced in language can understand without much effort as this facilitates maintenance.

Maybe what can give an optimized one is how key values are in the first array $keys .

If you do not need to save these values with the keys {} , then you do not have to have the keys. Once this is resolved, you could eliminate this process: $k = (int)$v; . It's a derisory economy but anyway, any millionth of a second is an advantage.

note: Another point is context analysis. Depending on where and how this code snippet is applied, often a revision in the structure may even prevent this process or there may be better ways to solve it. As I am oblivious to the conditions and circumstances, I refrain from commenting and kept my focus only on the code snippet proposed in the question.

    
12.07.2016 / 19:02