【问题标题】:How to compare inner array key and values and set the value of variable accordingly in PHP?如何比较内部数组键和值并在PHP中相应地设置变量的值?
【发布时间】:2015-03-25 14:48:42
【问题描述】:

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

                    )

我想要实现的是将 inner array 选项的键值与 outer array 中的键 [customValue] 的值进行比较,并在找到匹配时分配新变量 $education 的值。

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

$educaion = 'Freshman';

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

谢谢。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      相关资源
      最近更新 更多