I create a session array that will be used through out the site for each user. Once a user changes settings, the session array's settings change along with it.
I create a session array on load up of page:
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'
)
);
}
Once a user changes a setting, using an ajax call, I call a .php file to modify which ever associated setting is suppose to be changed:
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 will contain 'shell' or 'graphic-color' etc... to will contain the value that it is suppose to be, so none will change.
Now from their here is my code I have for modifying it:
$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;
Here is the output:
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 ) )
My question is, what am I doing wrong that it's adding to the end of the array instead of replacing, lets say [shell] to the value to which lets say is Img.test.png
array(..)[0] => Red-Metallic.png [] => Gold-Metallic.png )var_dump($key)see what it contains and how it can affect$_SESSION['controller']$_SESSION['controller']but in$_SESSION['controller']['color']or$_SESSION['controller']['graphics']array_searchwill not work with multidimensional arrays