How to completely wipe an array? [duplicate] - php

This question already has answers here:
What's better at freeing memory with PHP: unset() or $var = null
(13 answers)
Closed 7 years ago.
I have some sensitive data like user data and stuff stored in arrays at times.and the problem is that with a very high probability unset behaves just like deleting on most file systems work, just remove it from the index and well, "out of sight out of mind", but I clearly want to annihilate the arrays contents, so they cannot be read anymore even if there might be a second heartbleed or similar stuff... so I want to completely overwrite the array's data so it cannot be retrieved from the ram anymore after I finished my work with it.
any ideas?

To wipe an array completely, you can either unset or overwrite it.
$array = array(1, 2, 3);
// To unset it - a var_dump will return NULL afterwards
unset($array);
// To overwrite - a var_dump will return array(0) { empty } afterwards
$array = array();

Related

Set Array Equal to Another Array without Specified Key/Values [duplicate]

This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

checking if a value is a multidimensional array [duplicate]

This question already has answers here:
Checking if array is multidimensional or not?
(16 answers)
Closed 9 years ago.
How come when I run this code, I get an output of I am a multidimensional array! (the first block). I thought it would go into the second block, but it doesn't. What am I missing here?
$values = array('1','2');
if(isset($values[0][0])){
echo "I am a multidimensional array!";
}else{
echo "I am not a multidimensional array.";
}
$values = array(1,array(1,2));
$multi = false;
if(is_array($values)){
foreach($values as $k=>$v){
if(is_array($v)){
$multi = true;
break;
}
}
}
echo $multi ? "multi" : "not multi";
Try this:
if(is_array($values[0]))
Edit: This will check the first element of the array only. You should loop through each element to check if its truly multidimensional.
This code checks to see if the first element of the array is also an array. isset just checks whether or not a variable is NULL.
isset in your example is not working as expected. Perhaps there is a slight difference in functionality between PHP versions or setups. I didn't see anything in the manual but maybe you can:
http://php.net/manual/en/function.isset.php
Using is_array is more semantic, so in my opinion is a much better choice.
This code only goes into the if-branch for me, if the first value in the array is explicitly declared as a string,
$values = array('1',2);
– and with that the behavior is nothing but logical, because $values[0] is that text literal '1', and that has a first character that can be access using a zero based index.
So I guess either your real data is of a string type – or it maybe depends in the PHP version (I tested under 5.3.16).
Anyway, using is_array as the other answers already suggested is the right way to go here.

Php Unset All Keys [duplicate]

This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.

Store array in cookie [duplicate]

This question already has answers here:
Storing PHP arrays in cookies
(9 answers)
Closed 1 year ago.
I am converting the array into cookie by php serialize function
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", serialize($Promotedcart), time()+604800,'/');
And when the cookie is created then i am using the unserialize php function.
print_r(unserialize($_COOKIE['Promotedcart']));
it does not work.
When I print_R($_COOKIE) then it show me the value.
Cookies separated by semicolon. Serialized strings with arrays contain them inside. Maybe this is a problem. You can use base64 to avoid all possible escape issues.
You can use json_encode, json_decode functions to achieve this as an alternative.
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", json_encode($Promotedcart), time()+604800,'/');
$result = json_decode($_COOKIE['Promotedcart'], true);
print_r($result);
Give it a try, this should work.

Categories