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

时间:2015-01-27 03:40:27

标签: php arrays multidimensional-array associative-array key-value

我有一个名为$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';

我的问题是如何通过最少的比较来以编程方式进行?

感谢。

1 个答案:

答案 0 :(得分:1)

这可以给你一些想法

我使用了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