passing 2d array to a different page - php

hi
this is pushan
I am using $_SESSION['name']='the 2d array name', to assign a dynamically created 2d array to a session variable in php.when I am accessing the session variable in the other page anfd printing the values only the values of the last row of the 2d array is getting printed . the rest is all blank.Please help me I am under tremendous pressure.
thanks

It looks like you assign only a string to the session variable. But you have to assign the array itself:
$_SESSION['name'] = array(...);
// or a reference of the array
$_SESSION['name'] = $array2;

You can use serialize() to save the array to the session as a string, then unserialize() to recover it later.
Page 1:
$_SESSION['name'] = serialize($arrayName);
Page 2:
$arrayName = unserialize($_SESSION['name']);
var_dump($arrayName);

Related

Php access variable

In PHP i have array variable from another function like this
$v->params:
(
[{"username":"myusername","email":"myemail#gmail_com","phone":"0123456789","password":"abc123","fullname":"myfullname","register_ip":"127_0_0_1","country":"Qu\u1ed1c_Gia","birthday":"N\u0103m_sinh","gender":"male","bank_code":"Ng\u00e2n_h\u00e0ng","ip":"127_0_0_1","os":"Windows_10","device":"Computer","browser":"Mozilla_Firefox_77_0"}] =>
)
Now i want to access to it item, how can i code to access item value like this:
$password = $v->params->password; //myemail#gmail_com
I new with PHP thank you all
The data seems the wrong way round as it's the key of the array rather than a value.
So using array_keys()[0] to get the first key and then json_decode this...
$data = json_decode(array_keys($v->params)[0]);
you can then use the $data object to get at the values...
echo $data->username;

Passing a dynamic variable in an array within PHP

I am not sure if it's possible or not but I am trying to pass a random variable to fill an array.
Here is the code normally:-
//Loading all data of user in an array variable.
$user_fields = user_load($user->uid);
// Updated one array variable in this array.
$user_fields -> field_module_3_status['und'][0]['value'] = "Started";
// Saved back the updated user data
user_save($user_fields);
But I want to provide the variable field_module_3_status dynamically through a variable.
Hence suppose I have $user_field_name = field_module_3_status.
So what I tried to do is:-
$user_fields = user_load($user->uid);
$this_users_status = $user_fields -> $user_field_name;
$this_users_status['und'][0]['value'] = "Started";
user_save($user_fields);
Unfortunately this doesn't work.
Any idea how I can achieve this?
You're making a copy of the array when you do the assignment to $this_users_status. You need to assign to the array in the object property.
$user_fields->{$user_field_name}['und'][0]['value'] = "Started";
Or you could use a reference:
$this_users_status =& $user_fields->$user_field_name;

php multiple session name or sub-session name?

my script currect have two session called
$_SESSION['mypic']
and
$_SESSION['mypicsrc']
can I combine this two to one session and sub-session?
like this:
$_SESSION['mypic']
$_SESSION['mypic']['src']
the $_SESSION global is an array that will only store strings. If you want to store an array inside a $_SESSION var you have to serialize it first
$data = array( 'src' => '' );
$_SESSION['mypic'] = serialize($data);
then to get it back out you have to deserialize
$data = deserialize($_SESSION['mypic']);
However, you should store your data in a database and then store an id or reference to that particular record in $_SESSION.
Actually, you only have one session there, with the values stored in $_SESSION.
You can change them like any other variable;
$_SESSION['mypic']['src'] = $_SESSION['mypicsrc'];

Unable to assign value to php array

I am creating an array in php by assigning values retrieved from database. When I print the array it is displaying array as output and not its contents. However it does retrieve values from mysql.
$resultset=mysql_query("select isbn from tbl_book where publisherid='$publisherid'");
/***Retrieve Books*****/
while($resultISBNArray = mysql_fetch_assoc($resultset))
{
$isbn = $resultISBNArray["isbn"];
$myArr[]=$isbn;
}
echo $myArr
echoing any array always prints "Array". You need to pick individual values in the array (echo $myArr[0]) or use something like print_r().
You can not print an array. You have todo something like var_dump($myArr);

appending arrays into session variable php

Im trying to add additional arrays into my session variable such as...
$_SESSION[cart] .= array($_POST[name],$_POST[price],$_POST[quantity]);
All i get when i do this 3 times and var_dump is string(15) "ArrayArrayArray"
Youre using .= "." is for string concat so youre arrays are getting converted to strings you should use one of the following:
$_SESSION['cart'][] = array($_POST[name],$_POST[price],$_POST[quantity]);
$_SESSION['cart'] += array($_POST[name],$_POST[price],$_POST[quantity]);
array_push(array($_POST[name],$_POST[price],$_POST[quantity]), (array) $_SESSION['cart'];
You can use print_r and see the content of the array.
i.e., print_r($_SESSION)

Categories