0

我有一个关联数组,我想添加更多键和值

Array
(
    [0] => Array
        (
            [NUMBER] => 67
            [TYPE] => Other
            [DATE] => 3/31/2011
        )
     [1] => Array
          (
            [NUMBER] => 87
            [TYPE] => something
            [DATE] => 3/28/2011


          )
     [2] => Array
          (
            [NUMBER] => 67
            [TYPE] => Other
            [DATE] => 3/2/2011


          )

)

在上面的数组中,我想在 DATE 之前添加另一个名为 STATUS 和 value 的键,以便最终 iget

Array
(
    [0] => Array
        (
            [NUMBER] => 67
            [TYPE] => Other
            [STATUS] => waiting
            [DATE] => 3/31/2011
        )
}

可以请给我正确的方向

4

1 回答 1

2
$arr = Array(
          0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
          1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
          2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
       );

foreach($arr as $key => $value) {
   $arr[$key] = array_slice($value, 0, 2) + 
                array('Status' => 'waiting') + 
                array_slice($value, -1);
}

var_dump($arr);

给出以下数组:

array(3) {
  [0]=>
  array(4) {
    ["NUMBER"]=>
    int(67)
    ["TYPE"]=>
    string(5) "Other"
    ["Status"]=>
    string(7) "waiting"
    ["DATE"]=>
    string(9) "3/32/2011"
  }
  [1]=>
  array(4) {
    ["NUMBER"]=>
    int(87)
    ["TYPE"]=>
    string(9) "something"
    ["Status"]=>
    string(7) "waiting"
    ["DATE"]=>
    string(9) "3/28/2011"
  }
  [2]=>
  array(4) {
    ["NUMBER"]=>
    int(67)
    ["TYPE"]=>
    string(5) "Other"
    ["Status"]=>
    string(7) "waiting"
    ["DATE"]=>
    string(8) "3/2/2011"
  }
}
于 2013-05-03T21:46:10.727 回答