PHP Pass动态数组名称功能

时间:2012-03-29 23:02:30

标签: php arrays multidimensional-array

如何将数组键传递给函数以提取正确的键数据?

//数组

<?php

$var['TEST1'] = Array (
    'Description' => 'This is a Description',
    'Version' => '1.11',
    'fields' => Array(
            'ID' => array(
                    'type' => 'int',
                    'length' =>'11',
                    'misc' =>'auto_increment'
            ),
            'DATA' => array(
                    'type' => 'varchar', '
                    length' => '255'
            )
    );

$var['TEST2'] = Array (
            'Description' =? 'This is the 2nd Description',
            'Version' => '2.1',
            'fields' => Array(
                    'ID' => array(
                            'type' => 'int',
                            'length' =>'11',
                            'misc' =>'auto_increment'
                    ),
                    'DATA' => array(
                            'type' => 'varchar', '
                            length' => '255'
                    )
            )

//功能

<?php

$obj = 'TEST1';
print_r($schema[$obj]); // <-- Fives me output.  But calling the function doesn't.

echo buildStructure($obj);

/**
 * @TODO to add auto_inc support
 */

function buildStructure($obj)
{
    $output = '';
    $primaryKey = $schema["{$obj}"]['primary key'];

    foreach ($schema["{$obj}"]['fields'] as $name => $tag)
        // #### ERROR ####  Invalid argument supplied for foreach()
    {
        $type = $tag['type'];
        $length = $tag['length'];
        $default = $tag['default'];
        $description = $tag['description'];

        $length = (isset($length)) ? "({$length})" : '';
        $default = ($default == NULL ) ? "NULL" : $default;

        $output .= "`{$name}` {$type}{$length} DEFAULT {$default} COMMENT `{$DESCRIPTION}`, ";


    }
    return $output;
}

2 个答案:

答案 0 :(得分:2)

您的问题之后是令人难以置信的错误代码,但这里有一个固定代码,它返回您需要的结果:

<!-- language: lang-php -->
<?php
// How do I pass an array key to a function to pull up the right key's data?
$var['TEST1'] = Array (
    'Description' => 'This is a Description',
    'Version' => '1.11',
    'Fields' => Array(
            'ID' => array(
                    'type' => 'int',
                    'length' =>'11',
                    'misc' =>'auto_increment'
            ),
            'DATA' => array(
                    'type' => 'varchar', 
                    'length' => '255'
            )
    )
);
$var['TEST2'] = Array (
            'Description' => 'This is the 2nd Description',
            'Version' => '2.1',
            'Fields' => Array(
                    'ID' => array(
                            'type' => 'int',
                            'length' => '11',
                            'misc' =>'auto_increment'
                    ),
                    'DATA' => array(
                            'type' => 'varchar', 
                            'length' => '255'
                    )
            )
);


function buildStructure($obj)
{
    global $var;
    $output = '';
    // $primaryKey = $VAR[$obj]['primary key']; // Primary key is not 
    // defined in original array!!!
    foreach ($var[$obj]['Fields'] as $name => $tag){
        $type = $tag['type'];
        $length = $tag['length'];
        $default = (array_key_exists('default', $tag)) ? $tag['default'] : '';
        $description = (array_key_exists('description', $tag)) ? $tag['description'] : '';
        $length = (isset($length)) ? "({$length})" : '';
        $default = ($default == NULL ) ? "NULL" : $default;
        $output .= "`{$name}` {$type}{$length} DEFAULT {$default} COMMENT `{$description}`, ";
    }
    return $output;
}

$obj = 'TEST1';
echo buildStructure($obj);
// output is: 
// `ID` int(11) DEFAULT NULL COMMENT ``, `DATA` varchar(255) DEFAULT NULL COMMENT ``,
?>

答案 1 :(得分:1)

看起来好像有些不对劲:

  1. 您正在将数组放入$var,但该函数会在$skema
  2. 中查找
  3. 您的函数访问$ skema但该变量尚未定义
  4. 数组键为Fields而不是fields
  5. $obj = 'TEST1';
    $skema = array(
        'TEST1' => array(
            'Description' => 'This is a Description',
            'Version' => '1.11',
            'Fields' => Array(
                'ID' => array(
                        'type' => 'int',
                        'length' =>'11',
                        'misc' =>'auto_increment'
                ),
                'DATA' => array(
                        'type' => 'varchar', '
                        length' => '255'
                )
            )
        )
    );
    echo buildStructure($obj);
    
    /**
     * @TODO to add auto_inc support
     */
    
    function buildStructure($obj, $skema)
    {
        $output = '';
        $primaryKey = $skema[$obj]['primary key'];
    
        foreach ($skema[$obj]['Fields'] as $name => $tag)
        {
            $type = $tag['type'];
            $length = $tag['length'];
            $default = $tag['default'];
            $description = $tag['description'];
    
            $length = (isset($length)) ? "({$length})" : '';
            $default = ($default == NULL ) ? "NULL" : $default;
    
            $output .= "`{$name}` {$type}{$length} DEFAULT {$default} COMMENT `{$DESCRIPTION}`, ";
    
    
        }
        return $output;
    }