2016-11-20 1 views
0

実際にはRevolutionスライダの設定であるjsonオブジェクトの配列がデータベースに格納されています。文字列は、データベース・テーブルから取り出され、次のように変数を見てPHPに割り当てられた:phpとjsonオブジェクトの検索と値のチェック

$json = '[ 
     {"sliderType": "standard","options": ["standard","carousel","hero"]}, 
     {"sliderLayout": "auto","options": ["auto","fullwidth","fullscreen"]}, 
     {"responsiveLevels": 4096}, 
     {"gridwidth": 1200}, 
     {"gridheight": 700}, 
     {"autoHeight": "off","options": ["on","off"]}, 
     {"minHeight": 0}, 
     {"fullScreenOffsetContainer": ""}, 
     {"fullScreenOffset": "0"}, 
     {"delay": 9000}, 
     {"waitForInit": false,"options": [true,false]}, 
     {"disableProgressBar": "off","options": ["on","off"]}, 
     {"startDelay": 0}, 
     {"stopAfterLoops": -1}, 
     {"stopAtSlide": -1}, 
     {"viewPort": [ 
       {"enable": false,"options": [true,false]}, 
       {"outof": "wait","options": ["wait","pause"]}, 
       {"visible_area": "60%"} 
      ] 
     }, 
     {"lazyType": "smart","options": ["full","smart","single","none"]}, 
     {"dottedOverlay": "none","options": ["none","twoxtwo","threexthree","twoxtwowhite","threexthreewhite"]}, 
     {"shadow": 0,"options": [0,1,2,3,4,5,6]}, 
     {"spinner": "spinner0","options": ["off", "spinner0", "spinner1","spinner2", "spinner3", "spinner4", "spinner5"]}, 
     {"debugMode": false,"options": [true,false]}, 
     {"extensions": "extensions/"}, 
     {"extensions_suffix": ".min.js"}, 
     {"fallbacks": [ 
       {"panZoomDisableOnMobile": "off","options": ["on","off"]}, 
       {"nextSlideOnWindowFocus": "off","options": ["on","off"]}, 
       {"disableFocusListener": true,"options": [true,false]} 
      ] 
     }, 
     {"parallax": [ 
       {"type": "off","options": ["off","mouse","scroll","mouse+scroll"]}, 
       {"levels": [10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85]}, 
       {"origo": "enterpoint","options": ["slidercenter","enterpoint"]}, 
       {"speed": 400}, 
       {"bgparallax": "on","options": ["on","off"]}, 
       {"disable_onmobile": "off","options": ["on","off"]} 
      ] 
     }, 
     {"carousel": [ 
       {"horizontal_align": "center","options": ["left","right","center"]}, 
       {"vertical_align": "center","options": ["top","bottom","center"]}, 
       {"infinity": "on","options": ["on","off"]}, 
       {"space": 0}, 
       {"maxVisibleItems": 3}, 
       {"stretch": "off","options": ["on","off"]}, 
       {"fadeout": "off","options": ["on","off"]}, 
       {"maxRotation": 0}, 
       {"minScale": 0}, 
       {"vary_fade": "off","options": ["on","off"]}, 
       {"vary_rotation": "on","options": ["on","off"]}, 
       {"vary_scale": "off","options": ["on","off"]}, 
       {"border_radius": "0px"}, 
       {"padding_top": 0}, 
       {"padding_bottom": 0} 
      ] 
     }, 
     {"navigation": [ 
       {"keyboardNavigation": "on", "options": ["on","off"]}, 
       {"keyboard_direction": "horizontal", "options": ["horizontal","vertical"]}, 
       {"mouseScrollNavigation": "off", "options": ["on","off"]}, 
       {"onHoverStop": "on", "options": ["on","off"]}, 
       {"touch": [ 
         {"touchenabled": "on", "options": ["on", "off"]}, 
         {"swipe_treshold": 75}, 
         {"swipe_min_touches": 1}, 
         {"drag_block_vertical": false, "options": [true, false]}, 
         {"swipe_direction": "horizontal", "options": ["horizontal","vertical"]} 
        ] 
       }, 
       {"arrows": [ 
         {"style": ""}, 
         {"enable": false, "options": [true, false]}, 
         {"rtl": false, "options": [true, false]}, 
         {"hide_onmobile": false, "options": [true, false]}, 
         {"hide_onleave": true, "options": [true, false]}, 
         {"hide_delay": 200}, 
         {"hide_delay_mobile": 1200}, 
         {"hide_under": 0}, 
         {"hide_over": 9999}, 
         {"tmp": ""}, 
         {"left": [ 
           {"container": "slider", "options": ["slider","layergrid"]}, 
           {"h_align": "left", "options": ["left","center","right"]}, 
           {"v_align": "center", "otpions": ["top","center","bottom"]}, 
           {"h_offset": 20}, 
           {"v_offset": 0} 
          ] 
         }, 
         {"right": [ 
           {"container": "slider", "options": ["slider","layergrid"]}, 
           {"h_align": "left", "options": ["left","center","right"]}, 
           {"v_align": "center", "otpions": ["top","center","bottom"]}, 
           {"h_offset": 20}, 
           {"v_offset": 0} 
          ] 
         } 
        ] 
       }, 
       {"bullets": [ 
         {"style": ""}, 
         {"enable": false, "options": [true, false]}, 
         {"rtl": false, "options": [true, false]}, 
         {"container": "slider", "options": ["slider","layergrid"]}, 
         {"hide_onmobile": false, "options": [true, false]}, 
         {"hide_onleave": true, "options": [true, false]}, 
         {"hide_delay": 200}, 
         {"hide_delay_mobile": 1200}, 
         {"hide_under": 0}, 
         {"hide_over": 9999}, 
         {"tmp": "<span class=\"tp-bullet-image\"></span><span class=\"tp-bullet-title\"></span>"}, 
         {"direction": "horizontal", "options": ["horizontal","vertical"]}, 
         {"space": 0}, 
         {"h_align": "left", "options": ["left","center","right"]}, 
         {"v_align": "center", "otpions": ["top","center","bottom"]}, 
         {"h_offset": 20}, 
         {"v_offset": 0} 
        ] 
       }, 
       {"thumbnails": [ 
         {"style": ""}, 
         {"enable": false, "options": [true, false]}, 
         {"rtl": false, "options": [true, false]}, 
         {"container": "slider", "options": ["slider","layergrid"]}, 
         {"width": 100}, 
         {"height": 50}, 
         {"wrapper_padding": 2}, 
         {"wrapper_opacity": 1}, 
         {"wrapper_color": "#f5f5f5"}, 
         {"visibleAmount": 5}, 
         {"hide_onmobile": false, "options": [true, false]}, 
         {"hide_onleave": true, "options": [true, false]}, 
         {"hide_delay": 200}, 
         {"hide_delay_mobile": 1200}, 
         {"hide_under": 0}, 
         {"hide_over": 9999}, 
         {"tmp": "<span class=\"tp-thumb-image\"></span><span class=\"tp-thumb-title\"></span>"}, 
         {"direction": "horizontal", "options": ["horizontal","vertical"]}, 
         {"span": false, "options": [true, false]}, 
         {"position": "inner", "options": ["inner","outer-left","outer-right","outer-bottom","outer-top"]}, 
         {"space": 0}, 
         {"h_align": "left", "options": ["left","center","right"]}, 
         {"v_align": "center", "otpions": ["top","center","bottom"]}, 
         {"h_offset": 20}, 
         {"v_offset": 0} 
        ] 
       }, 
       {"tabs": [ 
         {"style": ""}, 
         {"enable": false, "options": [true, false]}, 
         {"rtl": false, "options": [true, false]}, 
         {"container": "slider", "options": ["slider","layergrid"]}, 
         {"width": 100}, 
         {"height": 50}, 
         {"wrapper_padding": 2}, 
         {"wrapper_opacity": 1}, 
         {"wrapper_color": "#f5f5f5"}, 
         {"visibleAmount": 5}, 
         {"hide_onmobile": false, "options": [true, false]}, 
         {"hide_onleave": true, "options": [true, false]}, 
         {"hide_delay": 200}, 
         {"hide_delay_mobile": 1200}, 
         {"hide_under": 0}, 
         {"hide_over": 9999}, 
         {"tmp": "<span class=\"tp-thumb-image\"></span><span class=\"tp-thumb-title\"></span>"}, 
         {"direction": "horizontal", "options": ["horizontal","vertical"]}, 
         {"span": false, "options": [true, false]}, 
         {"position": "inner", "options": ["inner","outer-left","outer-right","outer-bottom","outer-top"]}, 
         {"space": 0}, 
         {"h_align": "left", "options": ["left","center","right"]}, 
         {"v_align": "center", "otpions": ["top","center","bottom"]}, 
         {"h_offset": 20}, 
         {"v_offset": 0} 
        ] 
       } 
      ] 
     } 
    ]'; 

は私が必要とされているものはstdClassオブジェクトや配列の配列に変換することができます。 私はこれらのオブジェクトをすべて含んだページをレンダリングし、必要に応じて値を変更するためにX-editableを使用しました。値は次のようになり、単一のオブジェクトの新しい値を送信したときに、X-編集可能なプラグインので:

$levels = explode('-', $_POST['name']); 
$levels = array_reverse($levels); 
$newjson = ''; 
foreach($levels AS $level){ 
    if($newjson == ''){ 
     $newjson = '"'.$level.'":"'.$_POST['value'].'"'; 
    }else{ 
     $newjson = '"'.$level.'":[{'.$newjson.'}]'; 
    } 
} 
$newjson = '[{'.$newjson.'}]'; 

とはstdClassに変換します

再び
$_POST['name'] = 'navigation-arrows-left-container'; 
$_POST['value'] = 'customscontainer'; 
$_POST['pk'] = 1; 

私は、これらの値からJSONオブジェクトの文字列を構築することができていますオブジェクトまたは通常の配列。 しかし、インデックスが異なるので、新しい値がデフォルト値と同じかどうかをテストする方法はわかりません。私が必要とするのは、変更されたキーの値がデフォルトオブジェクトの値と異なるかどうかをチェックする関数です。異なる場合は、デフォルトと同じであればtrueを返し、falseを返します。

+1

サイドノート、自分で作るのではなく、 'json_encode()'を使って配列をjsonに変換するのはどうですか? 'json_encode()'が行の戻り値などを変換するので、エラーのための余地を残しているようです。 – Rasclatt

+0

元々配列として存在しません。私は手作業でデフォルトのJSON文字列を作成しました。回転スライダのドキュメントに基づいて、利用可能な設定とそのデフォルト値を知ることができました。利用可能なオプションを保持するオブジェクト "options"を追加しました。だから私は管理のいくつかのデフォルト値を変更し、特定のスライダーのためにそれらをレンダリングするときに、変更された値を持つオブジェクトは、デフォルト設定はスライダースクリプトのためにレンダリングする必要はないので変数に格納されます。 –

答えて

0

私はあなたが言っていることを理解していますが、ネストされた配列で対応する設定を探したい場合は、再帰関数を使用して特定のパターンまたはキー/値のペアを分離できます。次に例を示します。

/* 
** @description This function will recurse the array and find a pattern 
**    based on the supplied array 
*/ 
function search_array($array,$setting) 
    { 
     if(empty($setting)) 
      return $array; 

     foreach($array as $key => $value) { 
      if(is_array($value)) { 
       if(is_numeric($key)) { 
        $val = search_array($value,$setting); 
        if(!empty($val)) 
         $new[$key] = $val; 
       } 
       else { 
        if(is_array($setting) && isset($setting[0])) {    
         if($key == $setting[0]) { 
          unset($setting[0]); 
          $new[$key] = search_array($value,array_values($setting)); 
         } 
        } 
       } 
      } 
      else { 
       if(is_array($setting) && isset($setting[0])) { 
        if($setting[0] == $key) 
         $new[$key] = $value; 
       } 
      } 
     } 

     if(!empty($new)) { 
      return $new; 
     } 
    } 

上記の機能は、あなたにこの文字列navigation-arrows-left-containerに基づいて、この結果を与える:

Array 
(
    [26] => Array 
     (
      [navigation] => Array 
       (
        [5] => Array 
         (
          [arrows] => Array 
           (
            [10] => Array 
             (
              [left] => Array 
               (
                [0] => Array 
                 (
                  [container] => slider 
                 ) 

               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

) 

上記のように供給配列がある場合、それは次の機能

への入力を取得します
/* 
** @description This will flatten an array and find a specific key 
*/ 
function get_by_key($array,$search,&$save) 
    { 
     foreach($array as $key => $value) { 
      if($key == $search) { 
       $save[$key] = $value; 
      } 
      else { 
       if(is_array($value)) { 
        get_by_key($value,$search,$save); 
       } 
      } 
     } 
    } 
/* 
** @description This takes your post array and returns an array or false if it 
**    can't find the key 
*/ 
function get_setting($json,$POST) 
    { 
     # Set a storage array 
     $save  = array(); 
     # Explode the incoming string 
     $settings = explode('-',$POST['name']); 
     # Decode your json into an array for traversing 
     $array  = json_decode($json,true); 
     # Run the search 
     $new  = search_array($array,$settings); 
     # If empty, just stop 
     if(empty($new)) 
      return false; 
     # Continue on with processing searching for the proper key 
     get_by_key($new,array_pop($settings),$save); 
     # Return the results of that search 
     return (!empty($save))? $save : false; 
    } 

使用するには:

# The array input would be your $_POST array, I don't know how much you would 
# like to do with the one function 
print_r(get_setting($json,array('name'=>'navigation-arrows-left-container'))); 

同じnavigation-arrows-left-container文字列は、この最後の配列を与えるだろう与えられた以上の配列、:

Array 
(
    [0] => Array 
     (
      [container] => slider 
     ) 

) 

があなたの代わりに私が持っている2のただ一つの関数の中にいくつかのロジックを組み合わせることができ、それはより多くの仕事を取ります。これは続けるには十分であるはずです。

関連する問題