如何从另一个数组的值在PHP中构建多维数组的键?

时间:2022-05-02 01:34:05

I have a problem that I'm struggling to solve.

我有个问题要解决。

I have two arrays.

我有两个数组。

One is

一个是

$array1 = array('eligibility', 'eligibility_section');

But it could be an unknown length.

但是它的长度是未知的。

The other will hopefully contain the values above in its keys. Eg it might look like this.

另一个则希望在键中包含上面的值。它可能是这样的。

$array2 = array('eligibility' => array('eligibility_section' => 'some_val));

I need a way to check if the values in array1 exist as keys in array2, and they must exist in the same order. E.g. from the example above if the order of array1 was:

我需要一种方法来检查array1中的值在array2中是否作为键存在,并且它们必须以相同的顺序存在。例如,如果array1的顺序是:

array('eligibility_section', 'eligibility'); 

The method would return false.

该方法将返回false。

Sorry if its a stupid question but I'm very tired.

对不起,如果这是一个愚蠢的问题,但我很累。

2 个解决方案

#1


1  

This can be done with a simple array reduction:

这可以通过简单的数组还原来完成:

$array1 = array('eligibility', 'eligibility_section');
$array2 = array('eligibility' => array('eligibility_section' => 'some_val'));

$result = array_reduce($array1, function ($subject, $key) {
    return is_array($subject) && array_key_exists($key, $subject) ? $subject[$key] : false;
}, $array2);

echo $result ? 'true' : 'false';

With the PHP 7 null coalescing operator this can be shortened to this, though beware that your values must not be null for this to return the correct result:

使用PHP 7 null合并运算符,这个可以缩短为这个值,但是要注意,这个值必须为null才能返回正确的结果:

array_reduce($array1, function ($s, $k) { return $s[$k] ?? false; }, $array2)

#2


4  

Hopefully the following code will solve your problem.

希望下面的代码能解决您的问题。

$array1 = array('eligibility', 'eligibility_section', 'some_key');
$array2 = array('eligibility' => array('eligibility_section' => array('some_key' => 'some_val' ) ) );

function is_keys_exist( $array1, $array_to_compare ) {
    $i = 0;
    foreach( $array_to_compare as $key1 => $val1 ) {
        if( $key1 !== $array1[$i] ) {
            return false;
        }
        ++$i;
        if( is_array( $val1 ) ) {
            is_keys_exist( $array1, $val1 );
        }
    }
    return true;
}

var_dump( is_keys_exist( $array1, $array2 ) );

#1


1  

This can be done with a simple array reduction:

这可以通过简单的数组还原来完成:

$array1 = array('eligibility', 'eligibility_section');
$array2 = array('eligibility' => array('eligibility_section' => 'some_val'));

$result = array_reduce($array1, function ($subject, $key) {
    return is_array($subject) && array_key_exists($key, $subject) ? $subject[$key] : false;
}, $array2);

echo $result ? 'true' : 'false';

With the PHP 7 null coalescing operator this can be shortened to this, though beware that your values must not be null for this to return the correct result:

使用PHP 7 null合并运算符,这个可以缩短为这个值,但是要注意,这个值必须为null才能返回正确的结果:

array_reduce($array1, function ($s, $k) { return $s[$k] ?? false; }, $array2)

#2


4  

Hopefully the following code will solve your problem.

希望下面的代码能解决您的问题。

$array1 = array('eligibility', 'eligibility_section', 'some_key');
$array2 = array('eligibility' => array('eligibility_section' => array('some_key' => 'some_val' ) ) );

function is_keys_exist( $array1, $array_to_compare ) {
    $i = 0;
    foreach( $array_to_compare as $key1 => $val1 ) {
        if( $key1 !== $array1[$i] ) {
            return false;
        }
        ++$i;
        if( is_array( $val1 ) ) {
            is_keys_exist( $array1, $val1 );
        }
    }
    return true;
}

var_dump( is_keys_exist( $array1, $array2 ) );