Check the existence of the values of one array in the Keys of another

Asked

Viewed 97 times

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 the most correct and recommended in terms of performance and semantics, check the existence of array 1 values (without {} square brackets) in array 2 Keys (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 {...}
}

3 answers

2


You can use the function array_intersect() to know what 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]));

Exit:

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

Or even assemble 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]));

Exit:

Array
(
    [10] =>  var::create_terms >> 1::
    [28] => var::create_terms>>0::
)

1

I think I’d do something as simple as that:

$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 have not tested different techniques to be able 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 because it facilitates maintenance.

Perhaps what can be optimized is the way the key values are in the first array $keys.

If there is no need to store these values with the keys {}, then you don’t have to have the keys. Once this is solved, you could delete 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 piece of code is applied, often a revision in the structure can even avoid this process or there may be better ways to solve it. As I am oblivious to the conditions and circumstances, I refrained from commenting and remained focused only on the code snippet proposed in the question.

  • If you keep the same Keys, you can turn it into an array with array_keys() and then use array_compare() or array_intersect() to see if there are differences between one and the other.

  • I think it would consume more processes using these functions, @William Aparecido Brandino.

  • I agree, @Danielomine, there are several ways to check if one exists in another. This is a.

1

Can do only with a foreach, and go seeing from 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>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.