PHP Array_Push多维数组

时间:2012-02-27 07:07:00

标签: php arrays multidimensional-array array-push

 $m = new mysql();
 $players=$m->get('pugs','Players','PID=6');
 $players = unserialize($players);
$players = array($players);
array_push($players[0]['T1'],'test');

上面的代码有效,但我想这样工作(因为我不想要额外的数组层)

 $m = new mysql();
 $players=$m->get('pugs','Players','PID=6');
 $players = unserialize($players);
 array_push($players['T1'],'name5');

$ players是从我的数据库返回的数组(它在数据库中被序列化),

$players = array
    (
    'T1' => array
    (
    0 => "name1",
    1 => "name2",
    2 => "name3",
    3 => "name4"
    ),
    'T2' => array
    (
    0 => "name1",
    1 => "name2",
    2 => "name3",
    3 => "name4"
    ),
    'RDY' => array
    (
    ),
    'NRDY' => array
    (
    )
    );

请帮助我,我无法让它工作而不必将$ player定义为数组(即使它已经是一个???)

1 个答案:

答案 0 :(得分:1)

我无法重现您的问题

<?php
$m = new mysqlDummy();
$players=$m->get('pugs','Players','PID=6');
$players = unserialize($players);
var_dump($players);
array_push($players['T1'],'test');
var_dump($players);

class mysqlDummy {
    public function get($f, $t, $w) {
        return 'a:4:{s:2:"T1";a:0:{}s:2:"T2";a:0:{}s:3:"RDY";a:0:{}s:4:"NRDY";a:0:{}}';
    }
}

打印

array(4) {
  ["T1"]=>
  array(0) {
  }
  ["T2"]=>
  array(0) {
  }
  ["RDY"]=>
  array(0) {
  }
  ["NRDY"]=>
  array(0) {
  }
}
array(4) {
  ["T1"]=>
  array(1) {
    [0]=>
    string(4) "test"
  }
  ["T2"]=>
  array(0) {
  }
  ["RDY"]=>
  array(0) {
  }
  ["NRDY"]=>
  array(0) {
  }
}

(如预期的那样)