Get array's key recursively and create underscore separated string

后端 未结 5 750
走了就别回头了
走了就别回头了 2020-11-29 09:06

Right now i got an array which has some sort of information and i need to create a table from it. e.g.

Student{
      [Address]{
              [StreetAddress         


        
相关标签:
5条回答
  • 2020-11-29 09:09
    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;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 09:14

    Something like this maybe?

    $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);
    

    I know globals are bad, but can't think of anything better now.

    0 讨论(0)
  • 2020-11-29 09:17

    You can use the RecursiveArrayIterator and the RecursiveIteratorIterator (to iterate over the array recursively) from the Standard PHP Library (SPL) to make this job relatively painless.

    $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);
    

    The above example outputs something like:

    array (
      0 => 'Student_Address_StreetAddress',
      1 => 'Student_Address_StreetName',
      2 => 'Student_Marks1',
      3 => 'Student_Marks2',
    )
    
    0 讨论(0)
  • 2020-11-29 09:26

    Something like this works:

    <?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"
    }
    
    0 讨论(0)
  • 2020-11-29 09:35

    (Working on it, here is the array to save the trouble):

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

    Here it is, using a modified version of @polygenelubricants code:

    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>';
    

    Outputs:

    Array
    (
        [0] => Student_Address_StreetAddress
        [1] => Student_Address_StreetName
        [2] => Student_Marks1
        [3] => Student_Marks2
    )
    
    0 讨论(0)
提交回复
热议问题