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 ??? )

4
  • You need same clone of what you have in DB ? Commented Feb 27, 2012 at 7:17
  • yes i have a serialized version of that array i posted in the db, Commented Feb 27, 2012 at 7:19
  • 1
    just check this without array push $players['T1'][] ='name5' Commented Feb 27, 2012 at 7:24
  • codea: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 Commented Feb 27, 2012 at 7:25

1 Answer 1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.