unset

unset() static variable doesn't work?

筅森魡賤 提交于 2019-12-02 04:30:13
问题 See this code: http://codepad.org/s8XnQJPN function getvalues($delete = false) { static $x; if($delete) { echo "array before deleting:\n"; print_r($x); unset($x); } else { for($a=0;$a<3;$a++) { $x[]=mt_rand(0,133); } } } getvalues(); getvalues(true); //delete array values getvalues(true); //this should not output array since it is deleted Output: array before deleting: Array ( [0] => 79 [1] => 49 [2] => 133 ) array before deleting: Array ( [0] => 79 [1] => 49 [2] => 133 ) Why is the array, $x

Sessions get messed up on external host

旧城冷巷雨未停 提交于 2019-12-02 03:58:59
The problem sounds like this: The log-in using sessions works perfect on my localhost, but when the EXACTLY same files are uploaded to my host (hostgator), the sessions don't or, or they get messed up. Also the log-out feature doesn't work on the host. I've checked and every page has the session_start(); inside it. The session is not destroyed, even if my logout.php looks like this: <?php session_start(); $_SESSION = array(); session_unset(); session_destroy(); header("location:index.php"); exit(); ?> Any suggestions? I noticed on Firefox with Firebug that your pages are all cached . Your

unset() static variable doesn't work?

限于喜欢 提交于 2019-12-02 02:42:36
See this code: http://codepad.org/s8XnQJPN function getvalues($delete = false) { static $x; if($delete) { echo "array before deleting:\n"; print_r($x); unset($x); } else { for($a=0;$a<3;$a++) { $x[]=mt_rand(0,133); } } } getvalues(); getvalues(true); //delete array values getvalues(true); //this should not output array since it is deleted Output: array before deleting: Array ( [0] => 79 [1] => 49 [2] => 133 ) array before deleting: Array ( [0] => 79 [1] => 49 [2] => 133 ) Why is the array, $x not being deleted when it is being unset? If a static variable is unset, it destroys the variable only

Unsetting index in array turns it to an object

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 22:34:05
So I have a JSON document I'm storing in CouchDB. Here's the important part of it: "games": { "creator": [ "cf86d68b24c1bbf22702356572027642", "cf86d68b24c1bbf22702356572027dd8", "cf86d68b24c1bbf22702356572028b77" ], "solver": { } } I'm trying to remove one item from the array, let's say index 1: error_log("pre unset: " . json_encode($user->games)); unset($user->games->creator[1]); error_log("post unset: " . json_encode($user->games)); The problem is, it keeps converting my array to an object, like so: pre unset: {"creator":["cf86d68b24c1bbf22702356572027642","cf86d68b24c1bbf22702356572027dd8"

Recursive search and remove in array?

泄露秘密 提交于 2019-12-01 19:21:24
I am working with a multidimensional array I want to be able to remove an array (and all children) which match an id. The function I have tried is: function removeKey($key, $array, $childKey = 'children'){ if(isset($array[$key])){ unset($array[$key]); return $array; } foreach($array as &$item) if(isset($item[$childKey])) $item = removeKey($key, $item[$childKey], $childKey); return $array; } My example array is: Array ( [5] => Array ( [id] => 5 [parent_id] => [menu_title] => Drinks [page_title] => Drinks [status] => 1 [products] => 0 ) [1] => Array ( [id] => 1 [parent_id] => [menu_title] =>

unset variable in php

拟墨画扇 提交于 2019-12-01 03:17:12
I just read about unset variable through php manual. The php manual says "unset() destroys the specified variables" This def seems perfect until I came across static variable... "If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable. " This definition doesn't seems a good one for me, at least, since "destroy the variable" implies that the variable is no longer associated with that memory location. Does anyone else think a better definition would be "unset()

A recursive function to sort through parent and child nodes in PHP using a foreach loop on an array

雨燕双飞 提交于 2019-12-01 01:35:54
I have a data set stored in an array that references itself with parent-child ids: id , parent_id , title etc. The top tier has a parent_id of 0 , and there can be countless parent-child relationships. So I'm sorting through this array with a foreach loop within a recursive function to check each array element against its parent element, and I think I've been staring at this method too long. I do end up with the elements in the correct order, but I can't seem to get my lists nested correctly, which makes me think that the method doesn't really work. Is this the best route to take? What can I

PHP: remove element from multidimensional array (by key) using foreach

耗尽温柔 提交于 2019-11-30 20:32:06
I got multidimensional array. From each subarray, I would like to remove / unset values with index 1. My array $data. Array ( [3463] => Array ( [0] => 1 [1] => 2014 [context] => 'aaa' ) [3563] => Array ( [0] => 12 [1] => 2014 [context] => 'aaa' ) [2421] => Array ( [0] => 5 [1] => 2014 [context] => 'zzz' ) ) I would like to remove every element with index '1' from subarrays. Desired output is: Array ( [3463] => Array ( [0] => 1 [context] => 'aaa' ) [3563] => Array ( [0] => 12 [context] => 'aaa' ) [2421] => Array ( [0] => 5 [context] => 'zzz' ) ) Why this does not work? foreach ($data as $subArr

MongoDB : Update Modifier semantics of “$unset”

风流意气都作罢 提交于 2019-11-30 15:55:11
问题 In MongoDB, the update modifier unset works as follows: Consider a Mongo DB Database db with a collection users . Users contain a Document, of the following format: //Document for a user with username: joe { "_id" : ObjectId("4df5b9cf9f9a92b1584fff16"), "relationships" : { "enemies" : 2, "friends" : 33, "terminated" : "many" }, "username" : "joe" } If I want to remove the terminated key, I have to specify the $unset update modifier as follows: >db.users.update({"username":"joe"},{"$unset":{

MongoDB : Update Modifier semantics of “$unset”

我的梦境 提交于 2019-11-30 14:45:52
In MongoDB, the update modifier unset works as follows: Consider a Mongo DB Database db with a collection users . Users contain a Document, of the following format: //Document for a user with username: joe { "_id" : ObjectId("4df5b9cf9f9a92b1584fff16"), "relationships" : { "enemies" : 2, "friends" : 33, "terminated" : "many" }, "username" : "joe" } If I want to remove the terminated key, I have to specify the $unset update modifier as follows: >db.users.update({"username":"joe"},{"$unset":{"relationships.terminated": "many"}}); My Question is, why do I have to specify the ENTIRE KEY VALUE PAIR