php数组函数.试图避免拆包数组然后重新打包数组


php Array function. Trying to avoid unpacking array and then repacking array

是否有一种更简单的方法来将一个数组的一部分爆炸成一个额外的数组,而不是解包数组,运行爆炸函数,然后像下面这样重新打包数组?这很好,我想看看是否有更简单的方法。

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

你不能直接引用这个字段吗?

<?php
$myArray = Array ('field1' => "var1", 'field2' => "var2", 'field3' => "var3, var4, var5");
$myArray['field3'] = explode (",", $myArray['field3']);
print_r ($myArray);
?>

如果通过引用

遍历$terms,则不需要创建新数组
foreach($result as &$term) {
    $term['tables'] = explode(",",$term['tables']);
}

还可以看一下http://php.net/manual/en/control-structures.foreach.php

的第二个示例