当数组深度未知时动态检索值

时间:2014-01-07 21:40:49

标签: php arrays recursion

我正在尝试创建一个函数,根据对象的不同参数创建一个简单的key->值列表。只要每个对象都是扁平的,我就能正常工作,但我现在处于一种情况,我需要列表的值基于一个数组的属性。我可以强制它假设这只是一个深度,但我如何考虑变量数组深度?

我已经更改了它,以便value参数可以传递一个数组,该数组采用$ object-> array [0] [array [1]];

的形式

希望您能从代码中了解我正在尝试做什么:

function make_list($data, $value = 'name', $key = 'id'){
    $new_arr = array();
    if (is_array($data))
    {
      if (isset($data[0]->weight)){
        usort($data, function( $a, $b ){
          return $a->weight == $b->weight ? 0 : ( $a->weight > $b->weight ) ? 1 : -1;
        });
      } elseif (isset($data[0]->value)){
        usort($data, function( $a, $b ){
          return $a->value == $b->value ? 0 : ( $a->value > $b->value ) ? 1 : -1;
        });
      }
      foreach ($data as $key => $item) 
      {
        if (!is_object($item))
        {
            $item = (object)$item;
        }
        if(isset($item->{$key}))
        {
          if (is_array($value)){
            //account for nested value requests, assuming object properties that are arrays (seems to be what most of the database calls return) ['property', 'value']
            foreach ($value as $key => $val) {
              if($key == 0){

              } else {
                        //don't know what to do here!
              }
            }
                    //THIS WOULD GENERALLY WORK IF ARRAY LENGTH IS 2 -- BUT SYNTAX IS FUBARED
            $new_arr[$item->{$key}] = $item->{$value[0]}[{$value[1]}];
          } else {
                    //THIS IS THE OLD BEHAVIOUR THAT WAS WORKING WHEN PASSING A STRING
               $new_arr[$item->{$key}] = $item->{$value};
          }
        }
      }
    }
    return $new_arr;
  }

一旦我弄明白,我就会这样做,所以$ key也可以是一个数组。

$data = new Customer();
$data->name = 'John Smith';
$data->age = 21;
$data->contact = array('street'=>'Main St', 'number'=>56, 'city_id'=>1, 'city'=>array('name'=>'Dorset'));

//Want these behaviours to work:
make_list($data, array('contact', 'city', 'name');
make_list($data, array('contact', 'street');

make_list($ data,'age');

1 个答案:

答案 0 :(得分:2)

你可能会看一下递归。我不会在这里输入完整的解决方案,因为我不清楚你的意图是什么。但是,我将介绍典型递归函数的基本结构,以便了解它的使用方法。

function make_list_recursive($data, $input_key, $input_value) {
    $return_list = array();
    if(is_object($data)) {
        // iterate publicly accessible properties of object    
        foreach($data as $key => $value) {
            if (is_array($value)) {
                // iterate over array and recurse each item
                foreach($value as $array_val) {
                    $array_val_list = make_list_recursive($array_val, $input_key, $input_value);
                    $return_list = array_merge($return_list, $array_val_list);
                }
            } else {
                // here is where you perform your key/value matching logic
                // if the property matched your key/value criteria, then add it to $return_list
                $return_list[{some key}] = {some value};
            }
        }
    } else (is_array($data)) {
        // iterate through array and recurse each item
        foreach($data as $array_val) {
            $array_val_list = make_list_recursive($array_val, $input_key, $input_value);
            $return_list = array_merge($return_list, $array_val_list);
        }
    } else {
        throw new Exception('You must pass object or array to this function');
    }  
    return $return_list;
}