我声明了两个变量但未设置:
__var1=
__var2=
Run Code Online (Sandbox Code Playgroud)
现在我设置__var2有一些价值:
__var2=1
Run Code Online (Sandbox Code Playgroud)
当我尝试进行这样的检查时:
[ -z "$__var1" -a -z "$__var2" ] || echo "Both missing!"
Run Code Online (Sandbox Code Playgroud)
我收到了这条消息Both missing!。但这是不正确的。
这是为什么?以及如何进行正确的检查,看看它们是否都丢失了?
我正在使用Yii2并且刚刚开始在sessions其中使用。我已经在 Yii 网站上阅读了它们的文档。
我注意到的一件事是,在不使用标准超全局的情况下在会话中处理多维数组有点困难$_SESSION,因此我主要使用它。
我遇到困难的一件事是取消设置会话变量。
例子:
if (!Yii::$app->session->isActive) {
Yii::$app->session->open();
}
print_r($_SESSION['foo']);
if ($this->command == 'sample_action') {
if (!isset($_SESSION['foo'][$this->some_id][$this->example_id])) {
$_SESSION['foo'][$this->some_id][$this->example_id] = $this->example_id;
$result = true;
}
} elseif ($this->command == 'sample_action_2') {
if (isset($_SESSION['foo'][$this->some_id][$this->example_id])) {
unset($_SESSION['foo'][$this->some_id][$this->example_id]);
//$_SESSION['foo'][$this->some_id][$this->example_id] = ''; // This works
$result = true;
}
}
print_r($_SESSION['foo']);
Run Code Online (Sandbox Code Playgroud)
使用unset它根本不起作用,它仍然存在。但是,将其设置为空白值是可行的。
我有以下代码来设置数据:
MongoClient.connect(mongoConnectionUrl, function (err, db) {
if (err) {
console.log("error");
} else {
var collection = db.collection(collectionName);
collection.updateOne(query, {
"$set": data
}, function (err) {
if (err) {
console.log("eror");
} else {
console.log("success.........");
}
db.close();
});
}
});
Run Code Online (Sandbox Code Playgroud)
如何同时设置数据和取消设置某些字段?
我有以下数组:
\n\narray(5) { \n ["destino"]=> string(11) "op_list_gen" \n ["id_terminal"]=> string(0) "" \n ["marca"]=> string(2) "--" \n ["tipo"]=> string(2) "--" \n ["lyr_content"]=> string(14) "aawaw"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如何从数组中删除值“--”和空值?
\n\n我尝试使用 foreach 并删除未设置的元素,但它 \xc2\xb4s 不起作用。
\n\nforeach ($array as $key => $arra) {\n if(array_key_exists(\'--\', $array)){ \n unset($arra[$key]);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n 我不知道为什么这会引发错误 - 希望有人能看到这个问题?
$client->set('place', 'home');
$client->placeInfo();
$client->screen($client->response());
$client->unset('place');
Run Code Online (Sandbox Code Playgroud)
致命错误:调用未定义的方法Client :: unset()...
客户类
// stores the client data
private $data = array();
// stores the result of the last method
private $response = NULL;
/**
* Sets data into the client data array. This will be later referenced by
* other methods in this object to extract data required.
*
* @param str $key - The data parameter
* @param str $value - The data value
* @return $this - …Run Code Online (Sandbox Code Playgroud) 我有一个类,其中一些方法中设置了类变量.该类有一个__destruct()函数,它使用unset()函数取消设置类变量.
现在我列出了在__destruct中取消设置的所有变量,但似乎应该有一种方法来取消所有.
例如,现在我这样做:
function __destruct()
{
unset($this->variable1);
unset($this->variable2);
//et cetera
}
Run Code Online (Sandbox Code Playgroud)
肯定有这样的方法可以在不列出它们的情况下取消它们,对吗?
我正在写的是一个临时禁止脚本,适合那些喜欢用小僵尸网络纠缠我的网站的人.
我唯一的问题是如何取消设置json对象.
我有以下代码
/* JSON blocking script written by Michael Dibbets
* Copyright 2012 by Michael Dibbets
* http://www.facebook.com/michael.dibbets - mdibbets[at]outlook.com
* Licenced under the MIT license http://opensource.org/licenses/MIT
*/
// Turn on error reporting
ini_set("display_errors", 1);
error_reporting(E_ALL);
// Create our codeigniter filepath
$filepath = FCPATH.'pagemodules/blocked.json';
// Load file helper to be able to be lazy
$this->load->helper('file');
// Read the json file
$data = read_file($filepath,'c+');
// Have we succeeded? Then continue, otherwise fail graciously
if($data !== false)
{
// Let's make …Run Code Online (Sandbox Code Playgroud) 我的问题可能看似基本但仍然无法解决如何解决这个问题.
考虑一系列我最喜欢的水果
$array = array("Banana","Rasberry","Blackberry")
Run Code Online (Sandbox Code Playgroud)
我想清除这个数组,以便删除所有键和值.我的数组就像我写的那样是空的
$array = array();
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用array_push一些新数据.
我以为我可以,array_walk($array, unset($array[$key])但它不能正常工作.
我正在尝试使用数组$unset来取消设置关联数组$list值,如果其中一个属性$list['id'] 存在于数组中$unset.
但是,我很难理解这里的结果:
<?php
$list = array(
array(
'id' => 'foo'
),
array(
'id' => 'bar'
),
array(
'id' => 'baz'
),
array(
'id' => 'quix'
)
);
$unset = array(
'foo',
);
for ($i=0; $i < count($list); $i++) {
echo "Itteration: {$i}" . '<br />';
echo "Contest ID: {$list[$i]['id']}" . '<br />';
echo 'Not in Array: ', (!in_array($list[$i]['id'], $unset)) ? 'True' : 'False';
unset($list[$i]); // Trouble Section
echo …Run Code Online (Sandbox Code Playgroud) 我试图从test_bots.json中取消设置值并将其保存回来,但不知何故,数据格式在此过程中正在被更改.
test_bots.json包含此JSON数组:
["John","Vladimir","Toni","Joshua","Jessica"]
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
$good = 'Toni';
$good_arr = file_get_contents('test_bots.json');
$good_arr = json_decode($good_arr);
if(in_array($good, $good_arr)){
$key = array_search($good, $good_arr);
unset($good_arr[$key]);
$good_arr2 = json_encode($good_arr);
file_put_contents('test_bots.json',$good_arr2);
}
Run Code Online (Sandbox Code Playgroud)
保存的输出是:
{"0":"John","1":"Vladimir","3":"Joshua","4":"Jessica"}
Run Code Online (Sandbox Code Playgroud)
但我希望输出看起来像:
["John","Vladimir","Joshua","Jessica"]
Run Code Online (Sandbox Code Playgroud)
我尝试在保存数组之前反序列化数组,但它不起作用.
为什么会这样?