2013-07-06 109 views
1

我创建了一个会在整个站点中用于每个用户的会话数组。一旦用户更改设置,会话数组的设置随之一起更改。在数组中搜索数组中的值并替换 - 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

+0

'$ newArray [$键[0]' –

+0

没有不工作,它仍然会添加到数组的末尾。我的输出将是......'array(..)[0] => Red-Metallic.png [] => Gold-Metallic.png)' –

+0

'var_dump($ key)'看看它包含了什么,影响'$ _SESSION ['控制器']' –

回答

1

这里使用array_walk_recursive功能的解决方案:

$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; 
+0

谢谢你,作品完善! –

0

只要你的$to是一个数组,我只是这么做的:

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

这没有测试,但我希望它可以帮助! :)

0

您可以在此情况下

<?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"]); 
?>