1

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

8
  • No that does not work, it still adds to end of array. my output would be...array(..)[0] => Red-Metallic.png [] => Gold-Metallic.png ) Commented Jul 6, 2013 at 18:38
  • var_dump($key) see what it contains and how it can affect $_SESSION['controller'] Commented Jul 6, 2013 at 18:41
  • hmmm that is weird it's null Commented Jul 6, 2013 at 18:46
  • I think you should search not in $_SESSION['controller'] but in $_SESSION['controller']['color'] or $_SESSION['controller']['graphics'] Commented Jul 6, 2013 at 18:48
  • Cause array_search will not work with multidimensional arrays Commented Jul 6, 2013 at 18:50

3 Answers 3

1

Here's the solution:

$changeWhat = $_POST['what'];   // suppose it's 'graphics-color'
$to = $_POST['to'];
$newArray = $_SESSION['controller'];

$changeWhatKey = false; // assume we don't have changeWhat in $newArray
// next, iterate through all keys of $newArray
foreach ($newArray as $group_name => $group_options) {
    $changeWhatKeyExists = array_key_exists($changeWhat, $group_options);
    // if we have key $changeWhat in $group_options - then $changeWhatKeyExists is true
    // else it equals false
    // If we found the key - then we found the group it belongs to, it's $group_name
    if ($changeWhatKeyExists) {
        $changeWhatKey = $group_name;
        // no need to search any longer - break
        break;
    }
}

if ($changeWhatKey !== false)
    $newArray[$changeWhatKey][$changeWhat] = $to; // replace 

$_SESSION['controller'] = $newArray;
Sign up to request clarification or add additional context in comments.

Comments

0

aslong as your $to is an array, I would simply do:

$changeWhat = $_POST['what'];
$to = $_POST['to'];
$_SESSION['controller'][$changeWhat] = $to;

This isn't tested but I hope it helps! :)

Comments

0

You can use the array_walk_recursive function in this case

<?php
  $what = $_POST["what"];
  $to   = $_POST["to"];

  function my_callback( &$value, $key, $userdata ) {
    if ( $key === $userdata[0] ) {
      $value  = $userdata[1];
    }
  }

  array_walk_recursive( $_SESSION["controller"], "my_callback", array( $what, $to ) );

  print_r( $_SESSION["controller"] );
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.