将关联数组转换为索引数组

时间:2011-10-25 11:52:18

标签: php

我已经通过使用array_values看了几个例子,但是不能弄清楚如何让它工作......

我有一个通过POST传递的关联数组,我需要将它转换为索引数组...

我的print_r($ _ POST)给了我这个...我需要将所有这些放入索引数组中:)

Array (
    [fieldnames] => 36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202 
    [36771X21X198] => 3434343
    [display36771X21X198] => on
    [36771X21X199] => 5656565
    [display36771X21X199] => on 
    [36771X21X200] => 89898989 
    [display36771X21X200] => on 
    [36771X21X201] => 90909090 
    [display36771X21X201] => on 
    [36771X21X202] => 12121212 
    [display36771X21X202] => on 
    [move] => movesubmit 
    [move2] => ONLINE Submit 
    [thisstep] => 1 
    [sid] => 36771 
    [token] => 1234567890
) 

3 个答案:

答案 0 :(得分:73)

观察这种将$_POST转换为数字索引数组的神奇方法:

$numerical = array_values($_POST);


但是如果你想保留钥匙怎么办?也许你想要这样的东西?

$numerical = array();
$sep = ':';

foreach($_POST as $k=>$v)
{
  $numerical[] = $k.$sep.$v;
}

$ numeric将具有:

Array
(
    [0] => fieldnames:36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
    [1] => 36771X21X198:3434343
    [2] => display36771X21X198:on
    [3] => 36771X21X199:5656565
    [4] => display36771X21X199:on
    [5] => 36771X21X200:89898989
    [6] => display36771X21X200:on
    [7] => 36771X21X201:90909090
    [8] => display36771X21X201:on
    [9] => 36771X21X202:12121212
    [10] => display36771X21X202:on
    [11] => move:movesubmit
    [12] => move2:ONLINE Submit
    [13] => thisstep:1
    [14] => sid:36771
    [15] => token:1234567890
)


或者,对于我的最后一个例子:

$fieldnames_original = explode('|', $_POST['fieldnames']);
$fieldnames_actual = array();
$values = array();

foreach($_POST as $k=>$v)
{
  if($k!='fieldnames')
  {
    $fieldnames_actual[] = $k;
    $values[] = $v;
  }
}

将设置3个数组:

$ fieldnames_original:

Array
(
    [0] => 36771X21X198
    [1] => 36771X21X199
    [2] => 36771X21X200
    [3] => 36771X21X201
    [4] => 36771X21X202
)

$ fieldnames_actual:

Array
(
    [0] => 36771X21X198
    [1] => display36771X21X198
    [2] => 36771X21X199
    [3] => display36771X21X199
    [4] => 36771X21X200
    [5] => display36771X21X200
    [6] => 36771X21X201
    [7] => display36771X21X201
    [8] => 36771X21X202
    [9] => display36771X21X202
    [10] => move
    [11] => move2
    [12] => thisstep
    [13] => sid
    [14] => token
)

和$ values:

Array
(
    [0] => 3434343
    [1] => on
    [2] => 5656565
    [3] => on
    [4] => 89898989
    [5] => on
    [6] => 90909090
    [7] => on
    [8] => 12121212
    [9] => on
    [10] => movesubmit
    [11] => ONLINE Submit
    [12] => 1
    [13] => 36771
    [14] => 1234567890
)

答案 1 :(得分:1)

功能

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

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

答案 2 :(得分:0)

在小数组上测试的索引转换器的递归关联。

function assoc2indexedMulti($arr) {

    // initialize destination indexed array
    $indArr = array();

    // loop through source
    foreach($arr as $val) {

        // if the element is array call the recursion
        if(is_array($val)) {

            $indArr[] = assoc2indexedMulti($val);

        // else add the value to destination array
        } else {

            $indArr[] = $val;
        }
    }

    return $indArr;
}