php 将关联数组更改为索引数组/获取 Zend_Table_Row_Abstract 作为非关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1065131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:50:28  来源:igfitidea点击:

Change an associative array into an indexed array / get an Zend_Table_Row_Abstract as non-associative

phpzend-frameworkassociative-arrayassociative

提问by Ethan

Hi out there in Stackland. I was wondering if there was either a function or an easy way to change an associative array into an indexed array.

你好在斯塔克兰。我想知道是否有函数或简单的方法可以将关联数组更改为索引数组。

To elaborate, I'm using the Zend framework, and I've got a point in my site where I take out a row of an SQL table as an associative array. I've passed it to javascript via an echoed in JSON. However, I've noticed that I can see the names of my database's columns in Firebug. Having outsiders know the names of your tables and columns is a big security no-no, so I'd like to change it from

详细地说,我正在使用 Zend 框架,并且我在我的站点中有一个点,我将 SQL 表的一行作为关联数组取出。我已经通过 JSON 中的回显将它传递给 javascript。但是,我注意到我可以在 Firebug 中看到我的数据库列的名称。让外人知道你的表和列的名称是一个很大的安全禁忌,所以我想把它从

SQLarray[user_id]
SQLarray[block_id]
SQLarray[b_price] etc.

to

SQLarray[0]
SQLarray[1]
SQLarray[2] etc.

Is there a good way to do this?

有没有好的方法可以做到这一点?

It would also work to be able to have a Zend_Table_Abstract->fetchAll() return a non-associative array, but I don't think that's possible. Thanks for your help!

也可以让 Zend_Table_Abstract->fetchAll() 返回一个非关联数组,但我认为这是不可能的。谢谢你的帮助!

回答by Ian Elliott

Is pure php ok?

纯php可以吗?

$array = array_values($array);

Source

来源

回答by Ethan

define function

定义函数

function array_default_key($array) {
    $arrayTemp = array();
    $i = 0;
    foreach ($array as $key => $val) {
        $arrayTemp[$i] = $val;
        $i++;
    }
    return $arrayTemp;
}

Pass the associative array as a parameter and it will convert into the default index of the array. For example: we have Array('2014-04-30'=>43,'2014-04-29'=>41)after the call to the function the array will be Array(0=>43,1=>41).

将关联数组作为参数传递,它将转换为数组的默认索引。例如:Array('2014-04-30'=>43,'2014-04-29'=>41)在调用函数之后,数组将是Array(0=>43,1=>41)

回答by Kaloy

for multi layered array i use this:

对于多层阵列,我使用这个:



function getIndexedArray($array) {
        $arrayTemp = array();
        for ($i=0; $i < count($array); $i++) { 
            $keys = array_keys($array[$i]);
            $innerArrayTemp = array();
            for ($j=0; $j < count($keys); $j++) { 

                $innerArrayTemp[$j] = $array[$i][$keys[$j]];                
            }
            array_push($arrayTemp, $innerArrayTemp);
        }
        return $arrayTemp;
    }

it turns this:

它变成了这样:

(
    [0] => Array
        (
          [OEM] => SG
            [MODEL] => Watch Active2
            [TASK_ID] => 8
            [DEPT_ASSIGNED] => Purchashing  
        )
)

into this :

进入这个:

[0] => Array
        (
          [0] => SG
            [1] => Watch Active2
            [2] => 8
            [3] => Purchashing  
        )

回答by Hareesh Sivasubramanian

You could use this simple piece of code, if you do not want to use the inbuilt PHP function.

如果您不想使用内置的 PHP 函数,您可以使用这段简单的代码。

$input_array;           // This is your input array
$output_array = [];     // This is where your output will be stored.
foreach ($input_array as $k => $v){
    array_push($output_array, $v);
}
print_r($output_array);