1

有没有一种更简单的方法将一个数组分解成一个额外的数组,而不是解包数组,运行分解函数,然后重新打包数组,如下所示。这很好用,我试图找出是否有更简单的方法。

    public function getStandardizationTerms()
    {

        $statement = $this->db->query('SELECT * FROM '.$this->std_table);
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);

        $new_result = array();
        foreach($result as $term) {
            // the only purpose of this foreach loop is to turn the exception tables field into an array
            $new_result[] = array(              
                'key'          => $term['key'],
                'operator'     => $term['operator'],
                'fragment'     => $term['fragment'],
                'manufacturer' => $term['manufacturer'],
                'is_exception' => $term['is_exception'],
                'tables'       => explode(",",$term['tables'])
            );

        }       
        return $new_result;

    }   
4

2 回答 2

0

Couldn't you just reference the field directly?

<?php
$myArray = Array ('field1' => "var1", 'field2' => "var2", 'field3' => "var3, var4, var5");
$myArray['field3'] = explode (",", $myArray['field3']);
print_r ($myArray);
?>
于 2013-04-10T18:18:25.630 回答
0

$terms如果您通过引用循环低谷,则无需创建新数组

foreach($result as &$term) {
    $term['tables'] = explode(",",$term['tables']);
}

另请查看http://php.net/manual/en/control-structures.foreach.php上的第二个示例

于 2013-04-10T18:42:40.117 回答