我创建了一个会在整个站点中用于每个用户的会话数组。一旦用户更改设置,会话数组的设置随之一起更改。在数组中搜索数组中的值并替换 - PHP
我创建页面加载了一个会话阵列:
if (!isset($_SESSION['controller']))
{
$_SESSION['controller'] = array(
'color' => array(
'shell' => 'none',
'graphic-color' => 'none',
'part-color' => 'none'
),
'graphics' => array (
'text' => 'none',
'text-font' => 'none',
'text-style' => 'none',
'graphic' => 'none',
'part' => 'none'
)
);
}
一旦用户改变设置,使用Ajax调用,我称之为PHP文件要修改的相关有史以来设定假设是改变:
JS:
function changeSetting(what, to)
{
$.ajax({
url: "../wp-content/themes/twentytwelve/controller/php/controllerArrayMody.php",
type: "POST",
data: {
'what' : what,
'to' :to
},
success: function() {
}
});
}
what
将包含“壳”或“图形的颜色”等等...... to
将包含值,它是S无论如何,所以none
将改变。
现在是我的代码有修改它:
$changeWhat = $_POST['what'];
$to = $_POST['to'];
$newArray = $_SESSION['controller'];
$key = array_search($changeWhat , $_SESSION['controller']); // returns the first key whose value is changeWhat
$newArray[$key][0] = $to; // replace
$_SESSION['controller'] = $newArray;
这里是输出:
Array ([color] => Array ([shell] => none [graphic-color] => none [part-color]
=> none) [graphics] => Array ([text] => none [text-font] => none [graphic] =>
none [part] => none) [0] => Array ([0] => Red-Metallic.png))
我的问题是,我在做什么错,它的加入到可以说[shell]为值to
,可以说是Img.test.png
'$ newArray [$键[0]' –
没有不工作,它仍然会添加到数组的末尾。我的输出将是......'array(..)[0] => Red-Metallic.png [] => Gold-Metallic.png)' –
'var_dump($ key)'看看它包含了什么,影响'$ _SESSION ['控制器']' –