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;
Related
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;
I've just started out learning PHP/JSON and I've kind of worked out how to output an array from an json file. My aim is to output all the album titles in <li>'s (in this case they are called collectionName in the json file). I think I maybe going about it the wrong way though.
$artistId = '644708';
$otherAlbumsURL = 'http://itunes.apple.com/lookup?id='. $artistId .'&entity=album';
$a = (array)json_decode(file_get_contents($otherAlbumsURL));
var_dump($a);
If you want an array, just use:
$a = json_decode(file_get_contents($otherAlbumsURL), true);
var_dump($a);
Setting the second parameter in json_decode to TRUE will give you an associative array instead of an object.
Judging from the response of the URL, you'll need to loop through the result like this in order to get any available collection names (the first array element doesn't contain a collection name because it is information about the artist. i.e. it isn't an album):
$artistInfo = $a['results'][0]; //Assign artist info to its own variable.
unset($a['results'][0]); //Delete artist info from the array.
//Loop through the results
foreach($a['results'] as $result){
//$result['collectionName'] has the collection name.
echo $result['collectionName'] . '<br>';
}
I am little bit struggling in setting array in session.
Here is my code:-
Controller:
function taketest(){
$this->load->model('', '');
$questions_for_test = $this->session->userdata('questions_for_test');
$difficulty_level = $this->session->userdata('difficulty_level');
$question_id = $this->session->userdata('question_id');
if($question_id==""){
$question_id = $this->myjcat->returnRandomQuestion($questions_for_test,$difficulty_level);
$this->session->set_userdata('question_id',$question_id);
}
$question_details = $this->myjcat->getQuestion($question_id);
}
Model:
function returnRandomQuestion($questions_for_test, $difficulty_level){
$question_id = array_rand($questions_for_test[$difficulty_level], 1);
$used_question=array();
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
return $questions_for_test[$difficulty_level][$question_id];
}
But when I call:
$used_questions = $this->session->userdata('used_questions');
in controller
in the controller it will not return me an array.It gives me last value stored in it.
I could be misreading things, but it looks like you are only storing one value.
// this code:
$used_questions=$questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
// is the same as this code
$this->session->set_userdata('used_questions',$questions_for_test[$difficulty_level][$question_id]);
You're probably looking for this:
// fetch the stored copy first.
$used_questions = $this->session->userdata('used_questions');
if(!is_array($used_questions)) $used_questions = array();
// note the []
$used_questions[] = $questions_for_test[$difficulty_level][$question_id];
$this->session->set_userdata('used_questions',$used_questions);
You can set array values in session data like this :-
$this->session->set_userdata('used_questions', json_encode($used_questions));
And retrieve the data as :-
json_decode($this->session->userdata('used_questions'));
If you want the retrieved array data as associative array :-
json_decode($this->session->userdata('used_questions'), true);
Hope it hepls you :)
The problem is that $used_questions is storing the value stored in $questions_for_test[$difficulty_level][$question_id] and not the array.
So do this $this->session->set_userdata('used_questions',$questions_for_test);
This is because the data you pass to the $used_questions is a value.
You might want to do something like this:
array_push($used_questions, $questions_for_test[$difficulty_level][$question_id]);
*appending / adding a new value to an array
I am having a problem of how to organize my variables in flash that are from a PHP script.Ideally i want them in an array type format so i can loop through them.Below is some code to go with.
function completeHandler(evt:Event){ // after loading the php
var symbolsArray:Array = new Array()
symbolsArray.push(evt.target.data.symbol_1);// php variable named: symbol_1, symbol_2
trace(evt.target.data);
}
The above is allworking, the PHP variables are listed as symbol_1, symbol_2 etc
Instead of pushing each variable separably into the array i want a loop, along the lines of:
function completeHandler(evt:Event){
var symbolsArray:Array = new Array()
var counter =1
symbolsArray.push(evt.target.data.symbol_+counter); this is the issue
trace(symbolsArray[0]); //returns NaN
}
Below is the php return vars to flash to give an idea:
$returnVars['symbol_1'] = $virtualReel1[0];
$returnVars['symbol_2'] = $virtualReel1[1];
$returnVars['symbol_3'] = $virtualReel1[2];
$returnVars['symbol_4'] = $virtualReel2[0];
$returnVars['symbol_5'] = $virtualReel2[1];
//etc
$returnString = http_build_query($returnVars);
echo $returnString;
symbolsArray.push(evt.target.data["symbol_"+counter]);
If you need to dynamically query properties of an object, you address it as an Array or a Dictionary, by a string key, which can be dynamically formed. Works on anything.
The returned data can be treated as an Object (containing Objects) so you can loop thru it like so:
function completeHandler(evt:Event)
{
var symbolsArray:Array = new Array();
for each (var obj:Object in evt.target.data)
{
symbolsArray.push(obj);
}
}
If you know all items are oif same type, you can cast the object. eg: if all Numbers:
symbolsArray.push(Number(obj));
Or Strings:
symbolsArray.push(String(obj));
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);