如何从php中获取正常数组中的数组

时间:2014-02-25 21:25:27

标签: php arrays

我有一个这样的数组,它可以包含多个值:

Array
(
    [rpiid] => Array
        (
            [1] => 86
        )

    [sensor_id] => Array
        (
            [1] => 1
        )

    [when] => Array
        (
            [1] => 2014-02-24
        )

    [val] => Array
        (
            [1] => 000
        )

    [train] => Array
        (
            [1] => True
        )

    [valid] => Array
        (
            [1] => False
        )

    [button] => update
)

当然,这里每次只有1号,但有时候我有0,1,2和一个值相关联。这是因为我从多种形式的GET中得到了这个。

如何将此数组转换为

Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...

谢谢,

约翰。

3 个答案:

答案 0 :(得分:1)

这应该有效

$new_array = array();
foreach($first_array as $value => $key){
     $new_array[$key] = $value[1];
}

答案 1 :(得分:1)

如果你的数组是$ get

$newArray = Array();
foreach($get as $secondKey => $innerArray){
    foreach($value as $topKey => $value) {
        $newArray[$topKey][$secondKey] = $value;
    }
}

答案 2 :(得分:0)

当然可以,看看这个小例子:

$a = [ 'rpid' => [1], 'cpid' => [2,2] ];

$nodes = [];
foreach($a as $node => $array) { 
  foreach($array as $index => $value) { 
    if(empty($nodes[$index])) 
      $nodes[$index] = []; 

    $nodes[$index][$node] = $value; 
  }
}

的print_r($节点):

Array
(
    [0] => Array
        (
            [rpid] => 1
            [cpid] => 2
        )

    [1] => Array
        (
            [cpid] => 2
        )

)