PHP在连接键时将嵌套数组转换为单个数组?

时间:2009-12-10 18:07:07

标签: php arrays recursion multidimensional-array

这是一个示例数组:

 $foo = array(
           'employer' => array(
                    'name' => 'Foobar Inc',
                    'phone' => '555-555-5555'
                     ),
           'employee' => array(
                    'name' => 'John Doe',
                    'phone' => '555-555-5556',
                    'address' => array(
                           'state' => 'California',
                           'zip' => '90210'
                        )
                    ),
           'modified' => '2009-12-01',
         );

我希望得到这样的结果:

$fooCompressed = array(
             'employer_name' => 'Foobar Inc',
             'employer_phone' => '555-555-5555',
             'employee_name' => 'John Doe',
             'employee_phone' => '555-555-5556'
             'employee_address_state' => 'California',
             'employee_address_zip' => '90210',
             'modified' => '2009-12-01'
             )

我如何编写递归函数来处理这个问题?

7 个答案:

答案 0 :(得分:14)

这样的事情:

function makeNonNestedRecursive(array &$out, $key, array $in){
    foreach($in as $k=>$v){
        if(is_array($v)){
            makeNonNestedRecursive($out, $key . $k . '_', $v);
        }else{
            $out[$key . $k] = $v;
        }
    }
}

function makeNonNested(array $in){
    $out = array();
    makeNonNestedRecursive($out, '', $in);
    return $out;
}

// Example
$fooCompressed = makeNonNested($foo);

答案 1 :(得分:6)

我觉得这个'技巧'使用的是http_build_query不是一个带有递归的眼睛(或者至少让php为你做的)

如果你的str_replace使用[和]

的url编码值,那么

3行代码

$string      = http_build_query($array);
$string      = urldecode($string);
$string      = str_replace(
                    array('[',']'),
                    array('_','') , 
                    $string
                );
parse_str($string, $flat_array);

$ flat_array变为:

array(7) {
  ["employer_name"]         =>"Foobar Inc"
  ["employer_phone"]        =>"555-555-5555"
  ["employee_name"]         =>"John Doe"
  ["employee_phone"]        =>"555-555-5556"
  ["employee_address_state"]=>"California"
  ["employee_address_zip"]  =>"90210"
  ["modified"]              =>"2009-12-01"
}

答案 2 :(得分:3)

这是一个允许您通过第二个参数指定顶级前缀的函数:

function flatten_array($array, $prefix = null) {
  if ($prefix) $prefix .= '_';

  $items = array();

  foreach ($array as $key => $value) {
    if (is_array($value))
      $items = array_merge($items,  flatten_array($value, $prefix . $key));
    else
      $items[$prefix . $key] = $value;
  }

  return $items;
}

答案 3 :(得分:0)

/**
 * Flatten a multi-dimensional array or a nested object, constructing concatenated keys for
 *    nested elements.
 * @param array or object $array - the array or object to be flattened
 * @param array or string $key_path - current parent keys path.
 *    Pass this parameter as string if you need to set a common prefix for all keys 
 * @param string $level_separator - keys concatenation glue
 * @param array $flat - resulting flattened array (omit this parameter when calling the function)
 * @return single-dimensional array with all array keys as concatenated keys of elements' 
 *    paths through the data structure
 */
 function flattenArray($array, &$key_path = array(), $level_separator = '.', &$flat = array())
 {
      if(!is_array($key_path))
      {
           // sanitize key_path
           $key_path = array((string)$key_path);
       }
       foreach($array as $key => $value)
       {
            // push current key to path
            array_push($key_path, $key);

            if(is_array($value) || is_object($value))
            {
                 // next level recursion
                 $flat = array_merge($flat, flattenArray($value, $key_path, $level_separator, $flat));
             }
             else
             {
                  // write the value directly
                  $flat[implode($level_separator, $key_path)] = $value;
              }

              // remove used key
              array_pop($key_path);
        }

        return $flat;
  }

答案 4 :(得分:0)

经过几次迭代后,我已经能够改进这个问题的解决方案,该解决方案使用基于堆栈的方法来避免递归,简化了一些事情。

Notepad++

不完全是所要求的方法,但它与问题的递归方法形成鲜明对比。

答案 5 :(得分:0)

这将使多维关联数组变平坦,如果该数字重复,则将数字附加到密钥上。 如果您不介意使用数字索引来区分重复键而不是串联键,那么这可能是一个解决方案。

childEventListener

我怀疑它可以进一步用于创建级联密钥。

上面的解决方案基本上是做这种事情的

$result = array();
array_walk_recursive($your_array, function($v, $k) use (&$result){ $i = ""; for (; isset($result[$k."$i"]); $i++); $result[$k."$i"] = $v; });

答案 6 :(得分:0)

仅使用array_ * php函数+递归的解决方案:

$array = array(
    "level1"=>"value",
    "level2" => ["level11" => "value", "level21" => "value"],
    "level3" => ["level2" => ["level1" => "value"]],
    "level4" => ["level3" => ["level2" => ["level1" => "value"]]],
    "level5" => ["level4" => ["level3" => ["level2" => ["level1" => "value"]]]],
);
 
//flatten array with combined keys
function arrayFlat($array) {
    $result = [];
    array_walk($array, function($v, $pk)use(&$result){
        if(is_array($v)) {
            $result += arrayFlat(array_combine(
                array_map(function($k) use($pk){ return $pk . '_' .$k; }, array_keys($v)),
                $v
            ));
        } else {
            $result[$pk] = $v;
        }
    });
    return $result;
}

print_r(arrayFlat($array));

输出:

Array
(
    [level1] => value
    [level2_level11] => value
    [level2_level21] => value
    [level3_level2_level1] => value
    [level4_level3_level2_level1] => value
    [level5_level4_level3_level2_level1] => value
)