递归获取数组的键并创建下划线分隔的字符串

时间:2010-05-01 09:16:53

标签: php arrays multidimensional-array

现在我得到了一个包含某种信息的数组,我需要从中创建一个表。 e.g。

Student{
      [Address]{
              [StreetAddress] =>"Some Street"
              [StreetName] => "Some Name"
      }
      [Marks1] => 100
      [Marks2] => 50
    }

现在我想创建数据库表,其中包含字段名称:

Student_Address_StreetAddress
Student_Address_StreetName
Student_Marks1
Student_Marks2

它应该是递归的,所以从数组的任何深度它都可以用我的格式创建字符串。

5 个答案:

答案 0 :(得分:18)

您可以使用标准PHP库(RecursiveArrayIterator)中的RecursiveIteratorIteratorSPL(以递归方式迭代数组)来使这项工作相对轻松。

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$keys = array();
foreach ($iterator as $key => $value) {
    // Build long key name based on parent keys
    for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
        $key = $iterator->getSubIterator($i)->key() . '_' . $key;
    }
    $keys[] = $key;
}
var_export($keys);

以上示例输出如下内容:

array (
  0 => 'Student_Address_StreetAddress',
  1 => 'Student_Address_StreetName',
  2 => 'Student_Marks1',
  3 => 'Student_Marks2',
)

答案 1 :(得分:8)

(正在使用它,这是用于避免麻烦的数组):

$arr = array
(
    'Student' => array
    (
        'Address' => array
        (
            'StreetAddress' => 'Some Street',
            'StreetName' => 'Some Name',
        ),
        'Marks1' => '100',
        'Marks2' => '50',
    ),
);

在这里,使用@polygenelubricants代码的修改版本:

function dfs($array, $parent = null)
{
    static $result = array();

    if (is_array($array) * count($array) > 0)
    {
        foreach ($array as $key => $value)
        {
            dfs($value, $parent . '_' . $key);
        }
    }

    else
    {
        $result[] = ltrim($parent, '_');
    }

    return $result;
}

echo '<pre>';
print_r(dfs($arr));
echo '</pre>';

输出:

Array
(
    [0] => Student_Address_StreetAddress
    [1] => Student_Address_StreetName
    [2] => Student_Marks1
    [3] => Student_Marks2
)

答案 2 :(得分:1)

这样的事可能吗?

$schema = array(
    'Student' => array(
        'Address' => array(
            'StreetAddresss' => "Some Street",
            'StreetName' => "Some Name",
        ),
        'Marks1' => 100,
        'Marks2' => 50,
    ),
);

$result = array();

function walk($value, $key, $memo = "") {
    global $result;
    if(is_array($value)) {
        $memo .= $key . '_';
        array_walk($value, 'walk', $memo);
    } else {
        $result[] = $memo . $key;
    }
}

array_walk($schema, 'walk');

var_dump($result);

我知道全局变量很糟糕,但现在想不出更好的东西。

答案 3 :(得分:1)

这样的工作:

<?php

$arr = array (
  'Student' => array (
     'Address' => array (
        'StreetAddress' => 'Some Street',
        'StreetName' => 'Some Name',
     ),
    'Marks1' => array(),
    'Marks2' => '50', 
  ),
);

$result = array();

function dfs($data, $prefix = "") {
   global $result;

   if (is_array($data) && !empty($data)) {
      foreach ($data as $key => $value) {
        dfs($value, "{$prefix}_{$key}");
      }
   } else {
      $result[substr($prefix, 1)] = $data;
   }
}

dfs($arr);
var_dump($result);

?>

This prints

array(4) {
  ["Student_Address_StreetAddress"] => string(11) "Some Street"
  ["Student_Address_StreetName"] => string(9) "Some Name"
  ["Student_Marks1"] => array(0) {}
  ["Student_Marks2"] => string(2) "50"
}

答案 4 :(得分:0)

function getValues($dataArray,$strKey="")
{
    global $arrFinalValues;

    if(is_array($dataArray))
    {
        $currentKey = $strKey;
        foreach($dataArray as $key => $val)
        {
            if(is_array($val) && !empty($val))
            {   
                getValues($val,$currentKey.$key."_");
            }
            else if(!empty($val))
            {
                if(!empty($strKey))
                    $strTmpKey = $strKey.$key;
                else
                    $strTmpKey = $key;
                $arrFinalValues[$strTmpKey]=$val;
            }
        }
    }
}