2017-06-22 6 views
0

のJSONオブジェクトは、値が一致した場合にjsonオブジェクトを削除しようとしています。私は多くの方法を試みたが、childrenベースのオブジェクトにこだわっている。ここに私のJSONがどのように見えるかです:nestからキー値を見つけるPHPの

[{ 
     "id": "gx14jg", 
     "split": "", 
     "slug": "f4-1", 
     "url": "", 
     "name": "f4", 
     "type": "other_type_first-category", 
     "class": "undefined" 
    }, { 
     "id": "bo3m0q", 
     "split": 2, 
     "slug": "\/", 
     "url": "", 
     "name": "home", 
     "type": "page", 
     "class": "" 
    }, 
    [{ 
     "id": "g3qjsv", 
     "split": "", 
     "slug": "demo", 
     "url": "", 
     "name": "demo", 
     "type": "page", 
     "class": "", 
     "children": [{ 
      "id": "r318jh", 
      "url": "", 
      "slug": "f4-1", 
      "name": "f4", 
      "type": "other_type_first-category", 
      "class": "undefined", 
      "children": [{ 
       "id": "hztbje", 
       "url": "", 
       "slug": "f3", 
       "name": "f3", 
       "type": "other_type_first-category", 
       "class": "undefined" 
      }] 
     }] 
    }], 
    [{ 
     "id": "r9s608", 
     "split": "", 
     "slug": "demo2", 
     "url": "", 
     "name": "demo2", 
     "type": "category", 
     "class": "undefined", 
     "children": [{ 
      "id": "dlk12g", 
      "url": "", 
      "slug": "asddddddddd", 
      "name": "asddddddddd", 
      "type": "category", 
      "class": "undefined" 
     }] 
    }], 
    [{ 
     "id": "qg6c3p", 
     "split": 2, 
     "slug": "motorola", 
     "url": "", 
     "name": "Motorola", 
     "type": "product-category", 
     "class": "moto", 
     "children": [{ 
      "id": "rem23f", 
      "url": "", 
      "slug": "indian-resturants", 
      "name": "Indian Resturants", 
      "type": "Service-category", 
      "class": "undefined", 
      "children": [{ 
       "id": "kpwiq0", 
       "url": "", 
       "slug": "f4-1", 
       "name": "f4", 
       "type": "other_type_first-category", 
       "class": "undefined" 
      }] 
     }] 
    }] 
] 

は私が値「F3」と「スラグ」を見つけるとSLUGは、F3キーを保持している同じオブジェクトを削除する必要があります。

これまで私がこれまでに試したことは次のとおりです。 $ navはそのjson配列で、$slugf3です。

public function checkCategorySlugInNavigation($nav, $slug){ 

     $ArrayRemoved = []; 

     if(is_array($nav)){ 
      foreach($nav as $key=>$navVal){ 


       if($navVal->slug == $slug){ 
        unset($nav[$key]); 
       } 
       else{ 

        if(array_key_exists('children', $navVal)){ 

         $navVal = $this->checkCategorySlugInNavigation($navVal, $slug); 
        } 
        $ArrayRemoved[] = $navVal; 
       } 
      } 
     } else{ 

      if($nav->slug == $slug){ 
       unset($nav[$key]); 
      } else{ 
       if(array_key_exists('children', $nav)){ 

        $nav = $this->checkCategorySlugInNavigation($nav->children, $slug); 
       } 
       $ArrayRemoved[] = $nav; 
      } 
     } 

     return $ArrayRemoved; 
    } 

この場合、任意のサポート的な回答が役立ちます。

期待される成果は次のようになります。

[{ 
     "id": "gx14jg", 
     "split": "", 
     "slug": "f4-1", 
     "url": "", 
     "name": "f4", 
     "type": "other_type_first-category", 
     "class": "undefined" 
    }, { 
     "id": "bo3m0q", 
     "split": 2, 
     "slug": "\/", 
     "url": "", 
     "name": "home", 
     "type": "page", 
     "class": "" 
    }, 
    [{ 
     "id": "g3qjsv", 
     "split": "", 
     "slug": "demo", 
     "url": "", 
     "name": "demo", 
     "type": "page", 
     "class": "", 
     "children": [{ 
      "id": "r318jh", 
      "url": "", 
      "slug": "f4-1", 
      "name": "f4", 
      "type": "other_type_first-category", 
      "class": "undefined" 
     }] 
    }], 
    [{ 
     "id": "r9s608", 
     "split": "", 
     "slug": "demo2", 
     "url": "", 
     "name": "demo2", 
     "type": "category", 
     "class": "undefined", 
     "children": [{ 
      "id": "dlk12g", 
      "url": "", 
      "slug": "asddddddddd", 
      "name": "asddddddddd", 
      "type": "category", 
      "class": "undefined" 
     }] 
    }], 
    [{ 
     "id": "qg6c3p", 
     "split": 2, 
     "slug": "motorola", 
     "url": "", 
     "name": "Motorola", 
     "type": "product-category", 
     "class": "moto", 
     "children": [{ 
      "id": "rem23f", 
      "url": "", 
      "slug": "indian-resturants", 
      "name": "Indian Resturants", 
      "type": "Service-category", 
      "class": "undefined", 
      "children": [{ 
       "id": "kpwiq0", 
       "url": "", 
       "slug": "f4-1", 
       "name": "f4", 
       "type": "other_type_first-category", 
       "class": "undefined" 
      }] 
     }] 
    }] 
] 

は(事前に)ありがとうございます!

+1

期待される結果は何ですか? – axiac

+0

@axiacが最後に追加されました – user3201500

答えて

0

私はあなたの機能にいくつかの変更を加えました。代わりにrefrenceを使用しています。この解決策をチェックしてください。

public function checkCategorySlugInNavigation(&$nav, $slug){ //<---pass by reference so unset will effect on real result 

     $ArrayRemoved = []; 

     if(is_array($nav)){ 
      foreach($nav as $key=>$navVal){    

       if(isset($navVal->slug) && $navVal->slug == $slug){ 
        unset($nav[$key]);      
       } 
       else{ 

        if(is_array($navVal)) //<--- In your example children exist in array so need to check it also 
         $navVal = (isset($navVal[0]))?$navVal[0]:$navVal; 
        if(array_key_exists('children', $navVal)){ 
         $navVal = $this->checkCategorySlugInNavigation($navVal, $slug); 
        } 

       } 
      } 
      $ArrayRemoved[] = $nav; //<---- add array at end 
     } else{    
     if($nav->slug == $slug){     
      unset($nav[$key]); 
     } else{ 
      if(array_key_exists('children', $nav)){  
       $nav_tmp = $this->checkCategorySlugInNavigation($nav->children, $slug); //<-------update this 
       if(count($nav_tmp) == 0) 
        unset($nav->children); //<---- unset value 
      }     
     } 
    } 

     return $ArrayRemoved; 
    } 
} 
+0

試していただきありがとうございます、私はこれを私のコードに入れて、2分でお知らせしようとしています:) – user3201500

+0

それはほとんど働いていたので、配列内の唯一のオブジェクトです。だから私は "子供"もその唯一のオブジェクトを削除したい。 – user3201500

+0

編集したコードを確認してください。私は他の部分に変更を加えました –

0

試してみてください。シンプルで機能的です。

function remove_f3_children($array) { 

    foreach($array as $key => &$value) { 

     if(is_array($value) && isset($value[0]->children)) { 
      $val = remove_f3_children($value[0]); 

      if($val->children === null) 
       unset($value[0]->children); 
      else 
       $value = $val; 
     } 

     if(is_array($value) && ! isset($value[0]->children)) { 
      if(array_search('f3', (array) $value[0]) == 'slug') 
       $value = null; 
     } 

    } 

    return $array; 
} 

print_r(remove_f3_children(json_decode($json))); 

関数名を変更するだけです!

関連する問題