开发者

Can we do like this using PHP SESSION array?

开发者 https://www.devze.com 2023-02-01 08:09 出处:网络
Hi can we create two dimensional array using php session. If po开发者_开发百科ssible how to unset values randomly.No, PHP does not implement multi-dimensional arrays. However an element of an array ca

Hi can we create two dimensional array using php session. If po开发者_开发百科ssible how to unset values randomly.


No, PHP does not implement multi-dimensional arrays. However an element of an array can be an array itself. And any PHP data item can be stored in the session (however resources become meaningless outisde the thread they were initialized in, and objects require class definitions to be referenced from the session).

e.g.

<?php

$two_d=array(
    array(1,2,3),
    array(4,5,6),
    array(7,8,9),
    array('#','.','=')
    );
$two_d[3][2]='*'; // was '='

how to unset values randomly

This would be an oxymoron in a 2-dimensional array. But is perfectly valid in the context of an array of arrays:

unset($two_d[1]); // removed the whole second row from the above
unset($two_d[0][1]); // $two_d[0] is now array(1,3)


$_SESSION['whateverValue'] = Array(
  1 => Array (
    'a','b','c','d'
  ),
  2 => Array (
    'q','w','e','r','t'
  )
);

Voila, a two-dimensional array, in a session.

The session variables are in no way special while the script is executing. Their only "magic" is that they are unserialized at session_start() and serialized at session_close()

0

精彩评论

暂无评论...
验证码 换一张
取 消