2013-07-06 157 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"]); 
?>