根据值PHP查找键

时间:2013-09-27 06:56:03

标签: php multidimensional-array

[items] => Array
    (
        [0] => Array
            (
                [product_option_id] => 328
                [option_id] => 26
                [name] => Product Type
                [type] => checkbox
                [group_no] => 1
                [option_value] => Array
                    (
                        [0] => Array
                            (
                                [product_option_value_id] => 1473
                                [option_value_id] => 68
                                [option_boolean_value] => True
                                [option_cost] => 10.0000
                                [option_enable] => 1
                                [apply_default] => 1
                                [apply_option] => 0
                            )

                        [1] => Array
                            (
                                [product_option_value_id] => 1474
                                [option_value_id] => 151
                                [option_boolean_value] => False
                                [option_cost] => 0.0000
                                [option_enable] => 1
                                [apply_default] => 0
                                [apply_option] => 0
                            )

                    )

                [required] => 1
                [optionapply] => A
                [booleanvalue] => True
                [applyheading] => 1
                [flagheading] => 0
                [dhead_id] => 0
                [routes_dephead_id] => Array
                    (
                        [0] => 44
                        [1] => 37
                    )

                [routes_id] => Array
                    (
                        [0] => 948
                        [1] => 949
                    )

            )

如何获得option_value_id为68的密钥。这里68不是静态的,每次动态我想要动态解决方案。

2 个答案:

答案 0 :(得分:0)

<?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

http://php.net/manual/en/function.array-search.php

答案 1 :(得分:0)

试试这个。

function search_id($array, $id) 
    foreach ($array['items'] as $key => $val) {
        foreach($val['option_value'] as $option_key => $option_value)
        {
             if ($option_value['option_value_id'] == $id) 
             {
                 return $option_key;
             }
        }
   }