如何在 PHP 中比较内部数组键和值并相应地设置变量的值


How to compare inner array key and values and set the value of variable accordingly in PHP?

我有以下一个名为$UserProfile的大数组,如下所示:

Array
(
[cf_classification] => Array
        (
            [field_id] => 10
            [field_name] => classification
            [customValue] => 11
            [cg_user_group_id] => 0
            [options] => Array
                (
                    [11] => Array
                        (
                            [value] => Freshman
                        )
                    [12] => Array
                        (
                            [value] => Shopomore
                        )
                    [13] => Array
                        (
                            [value] => Junior
                        )
                    [14] => Array
                        (
                            [value] => Senior
                        )
                    [15] => Array
                        (
                            [value] => Masters
                        )
                    [16] => Array
                        (
                            [value] => Ph.D
                        )
                )
        )
)

现在,如果您从上面的数组中看到一个键值对,如下所示:

[customValue] => 11

在上面的数组中,还有一个称为options的内部级别数组,如下所示:

[options] => Array
                    (
                        [11] => Array
                            (
                                [value] => Freshman
                            )
                        [12] => Array
                            (
                                [value] => Shopomore
                            )
                        [13] => Array
                            (
                                [value] => Junior
                            )
                        [14] => Array
                            (
                                [value] => Senior
                            )
                        [15] => Array
                            (
                                [value] => Masters
                            )
                        [16] => Array
                            (
                                [value] => Ph.D
                            )
                    )

我想要实现的是将内部数组选项的键值与外部数组的键[customValue]值进行比较,并在匹配时发现将值分配给新变量$education

简而言之,在上述情况下$education应该具有值'Freshman'因为 11 是匹配的键和值。所以所需的输出将是

$educaion = 'Freshman';

我的问题是如何通过进行最少数量的比较以编程方式做到这一点?

谢谢。

这可以

给你一些想法

我使用方法array_keys_exists($key,$array)检查阵列上是否存在此键

你也可以看看这个http://php.net/manual/en/ref.array.php

<?php

$user_profile = array(
    'cf_classification' => array(
        'field_id' => 10,
        'field_name' => 'classification',
        'customValue' => 11,
        'cg_user_group_id' => 0,
        'options' => array(
                11 => array(
                        'value' => 'Freshman'
                    ),
                12 => array(
                        'value' => 'Shopomore'
                    ),
                13 => array(
                        'value' => 'Junior'
                    ),
                14 => array(
                        'value' => 'Senior'
                    ),
            15 => array(
                        'value' => 'Masters'
                    ),
        16 => array(
                        'value' => 'Ph.D'
                    )
            )
    )
);

$custom_field = $user_profile['cf_classification']['customValue'];
$education = '';
$options = $user_profile['cf_classification']['options'];
# check if the $custom_field is in the options of user_profile['cf_classification']
# using the array_key_exist method
if (array_key_exists($custom_field, $options)) {
    # to get the value of the custom field then you need to traverse
    # the array for its value
    $education = $options[$custom_field]['value'];
} else {
    $education =  "not found";
}
echo $education;

这将打印

Freshman