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

This code above works but i want to to work like this ( because i dont want an extra array layer )

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

the $players is an array that is returned from my db ( it is kept serialized in the db ),

$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
    (
    )
    );

please help me, i cant get it to work without having to define $players as an array ( even tho it already is one ??? )

twizzle
  • 11
  • 2
  • You need same clone of what you have in DB ? – Duke Feb 27 '12 at 07:17
  • yes i have a serialized version of that array i posted in the db, – twizzle Feb 27 '12 at 07:19
  • 1
    just check this without array push $players['T1'][] ='name5' – Duke Feb 27 '12 at 07:24
  • `code`a:4:{s:2:"T1";a:0:{}s:2:"T2";a:0:{}s:3:"RDY";a:0:{}s:4:"NRDY";a:0:{}}`code` is the serialized info in the db, the sql part of the code is not the problem its array_push into an array with more than one level – twizzle Feb 27 '12 at 07:25

1 Answers1

1

I cannot reproduce your problem

<?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:{}}';
    }
}

prints

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) {
  }
}

(as expected)

VolkerK
  • 95,432
  • 20
  • 163
  • 226