$_Session Array unidentified index - php

I have class named Country->Gov->Branch. Each one of them returns an array to the other in this order : Country gives Gov and merges it with gov, gov gives branch and merges it with branch. Branch array is an attribute of User class when I use User and load it into $_session , I do merge the Branch array to session directly using array merge to make it easy to use so I dont have to loop array of array (Just an idea). The result of print_r($_SESSION); was as follows
Array ( [userid] => 1
[login] => email#gmail.com
[name] => Mohamed Talaat
[group] => 0
[picture] => mac
[Branchbranchid] => 1
[Branchbranchname] => Head Office
[Branchaddress] => xxxxxx st. , xxxx
[Branchgovid] => 2
[Govgovid] => 2
[Govgovcode] => 03
[Govgovname] => Alexandria
[Govcountryid] => 1
[Countrycountryid] => 1
[Countrycountrycode] => 20
[Countrycountryname] => Egypt )
I said Horray , but the happy feelings don`t last forever when I call it in the way below, I get an error.
echo $_SESSION['Branchbranchid'];
Notice: Undefined index: Branchbranchid in D:\xampp\htdocs\office\home.php on line 7
and nothing is echoed
I do have a session_start(); at the first head of the page before any code , i notice the problem happened at the merged part starting from the key [Branchbranchid] because if i do echo $_session['any element']; like
echo $_SESSION['name'];
Before this key ([Branchbranchid]), the code works fine. I tried to turn around by adding values with keys to another array but I get the same result and the same notice appear and nothing is echoed. I think there is a problem in the keys because when I have pushed the values to an indexed array, its working fine but without the keys, it is a lost case ...
Any ideas ... about why and How to avoid or to fix this issue

You should put start_session() as first line in your php file wherever you are trying to access the global session.
And change the $_session with $_SESSION

Related

PHP, getting Undefined property: stdClass::$id but it definitely exists

So it's really simple code and I am yanking my hair out with the problem.
//I first retrieve some JSON info (confirmed to work fine)
$file=file_get_contents('url');
//I then decode and print to verify (still working)
$somename=json_decode($file);
I print it out just to make sure it works (it does):
print_r($somename);
The print out reads as follows:
stdClass Object ( [id] => 456456456 [name] => somename [Stuff01] => 55 [Stuff02] => 25 [Stuff03] => 123132123132 ) )
Now I just want to get the value in the 'id' key so I use the appropriate object call:
$thisID=$somename->{'id'};
But I get the error:
Notice: Undefined property: stdClass::$id
I print_r every time so I know it's there. I can see it. What am I doing wrong?
I have had no problems doing this exact thing many times.
How do you access to individual properties is based on your data structure. You have kind of nested structure, object in object. Try like this:
$somename->somename->id;
//or
$yourObjectName->somename->id;
I hope this helps!

Can't use method return value in write context

I am using codeingiter platform to build an application. I am facing an error while trying to use unset() function in a controller. I have an array which looks like:
Array ( [userfile] => Screenshot from 2014-11-02 21:36:32.png
[pause_flag] => 0 [merchant_top] => 0 [category_id] => 1
[category_id1] => 1 [category_id2] => 1 )
While trying to unset 'userfile' from the array using unset($this->input->post('userfile'));
I am getting an error which suggests Can't use method return value in write context.
Please help me to resolve this issue
unset is used for unset the content of a variable or array, but you are passing a method return insted a variable or array.

Having trouble extracting data from PDO array

It might be the late hour or user error but I'm having trouble extracting variables from a PDO, where it would normally work. When I print_r my results by doing $array->results(), I get the following line:
Array ( [0] => stdClass Object ( [messageid] => 1 [fromid] => 2 [toid] => 1 [message] => Hello! ) )
However, on other projects when working with other people I have then just referred to the variable like this:
$result['message']
This is obviously after the line
$result = $array->results();
I've been troubleshooting and come to no conclusions, but I'm terrible at managing arrays so it could be something super simple. Any ideas? Thanks!
Since the result seems objects within array, so get your array and access object(s) inside it, to get message object from your $result, just do:
echo $result[0]->message;

Show array as written with rand inside

I have an array such as:
$var = array('hi','ho',rand(2,5));
What I would like to echo is the entire array, exactly as written.
Normally when you try a print_r, it shows as:
Array (
[0] => hi
[1] => ho
[2] => 3
)
But I want:
Array (
[0] => hi
[1] => ho
[2] => rand(2,5)
)
You can get this with file_get_contents, but is there any way to do so within the actual PHP file?
I don't think it's possible because when array is created, random value is assigned to element with index 2 and you cannot check how this value was created.
I don't think it's possible, since the rand is already evaluated as soon as you set the array to some variable.
A workaround would be the hold the expression as a string and then eval it when you need it. Like this:
$varStr = "array('hi','ho',rand(2,5))";
echo $varStr;
// when you actually need it
$var = eval($varStr);
However, this is almost never a good idea. Providing a use-case where you need this might help come up with a better solution.

CakePHP returning double array from find('list) query

I'm using cakephp and am getting back a "double array" where it is giving me 2 arrays where it should be 1, I have looked into the issue as far as cakephp and can't figure it out and just want to move past this for now so I am wondering if anyone knows how to unset a second array if a variable has 2 arrays.. below is the print_r of the array, its just one variable that has this, which I find odd.. so I want to make it so there is not a 2nd set of duplicate values, if I do an array_push it pushes both values for that index into the resulting new array index so that won't work
one variable is equal to the following:
Array ( [0] => 42 [1] => 62 ) Array ( [0] => 42 [1] => 62 )
EDIT:
This is not an issue of my printing out the array twice accidentally, as I said above, with a foreach array_push of the variable, i end up with this, which is odd:
Array ( [0] => 4242 [1] => 6262 )
EDIT:
This is the cakephp database call that I am using, I know I didn't ask this in regards to cakephp but since some people think this is impossible i am posting this just so you can see what it does if you want
$specificfields_array = $this->Mymodel->find('list', array('fields' =>'Mymodel.id'),
'conditions' => array('emailgroup' => $categorynumber, 'sent' => '0');));
EDIT:
This is what a "foreach" array_push is:
$mynewarray = array();
foreach ($specificfields as $specificfields_current) {
array_push ($mynewarray, $specificfields_current);
}
A variable cannot "have two arrays". It can be one array that has two arrays nested. The scenario you describe is impossible (probably there are two print_r there or there is a < character hiding stuff – check the HTML source).
Can you post the controller, the model and the view file with your print_r calls to the http://bin.cakephp.org/ site and post the links back here so we can see all of your code?

Categories