当我执行数组推送时,出现数组到字符串转换错误。但是array_merge不起作用。
/page1.php /
我从上一页的表单中获取值,$ _ POST ['checkBox']被设置为数组。
if (isset($_POST['checkBox'])) {
$checkBoxValue = $_POST['checkBox'];
if (!$session->exists('results')){
$session->set('results', $checkBoxValue);
}else if ($session->exists('results')){
$session->add('results', $checkBoxValue);
}
header('Location: export.php');
}
if (!$session->exists('results')) {
echo "U heeft geen adviezen gekozen.";
} else if ($session->exists('results')) {
echo "<b>U heeft de volgende adviezen gekozen: </b><br>";
foreach ($session->get('results') as $value) {
echo $value.'<br>';
}
}
/ $ session-> add()/
这是我的array_push方法。
public static function add($key, $value){
if (isset($_SESSION[$key])){
array_push($_SESSION[$key], $value);
}
}
/错误/
我print_r我的会话,这是我得到的错误:
Notice: Array to string conversion in ...
Array
(
[login] => 1
[data] => 1
[results] => Array
(
[0] => Lorem ipsum
[1] => Lorem
[2] => Lorem ipsum
[3] => Array
(
[0] => Lorem
)
)
)
答案 0 :(得分:1)
我认为您在这里错了。变量<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<form>
<div class="form-group">
<label class="control-label"></label>
<input type="text" value="" class="myCars">
</div>
</form>
<form>
<div class="form-group">
<label class="control-label"></label>
<input type="text" value="0011" class="myCars">
</div>
</form>
</div>
可能是数组类型,但是Session中的$checkBoxValue
不是数组。要将其用作数组,必须将其声明为数组。以下代码可以解决该问题:
results
希望这会有所帮助。
答案 1 :(得分:0)
如果您要使用array_push()将“ $ key” =>“ $ value”对插入到数组中,可以使用以下方法完成:
$_SESSION[$key] = $value;
没有必要使用array_push