2017-03-07 46 views
-5
{ 
    "status": "success", 
    "data": { 
     "title": "test2", 
     "json_query": { 
      "condition": "AND", 
      "rules": [{ 
       "id": "event", 
       "field": "event", 
       "type": "string", 
       "operator": "equal", 
       "value": "signup" 
      }, { 
       "condition": "AND", 
       "rules": [{ 
        "id": "event", 
        "field": "event", 
        "type": "string", 
        "operator": "equal", 
        "value": "signup" 
       }, { 
        "condition": "AND", 
        "rules": [{ 
         "id": "kwd", 
         "field": "kwd", 
         "type": "string", 
         "operator": "equal", 
         "value": "epub" 
        }] 
       }] 
      }, { 
       "id": "kwd", 
       "field": "kwd", 
       "type": "string", 
       "operator": "equal", 
       "value": "arts" 
      }, { 
       "condition": "AND", 
       "rules": [{ 
        "id": "kwd", 
        "field": "kwd", 
        "type": "string", 
        "operator": "equal", 
        "value": "automotive" 
       }, { 
        "condition": "AND", 
        "rules": [{ 
         "id": "kwd", 
         "field": "kwd", 
         "type": "string", 
         "operator": "equal", 
         "value": "books" 
        }, { 
         "condition": "AND", 
         "rules": [{ 
          "id": "kwd", 
          "field": "kwd", 
          "type": "string", 
          "operator": "equal", 
          "value": "business" 
         }, { 
          "condition": "AND", 
          "rules": [{ 
           "id": "kwd", 
           "field": "kwd", 
           "type": "string", 
           "operator": "equal", 
           "value": "books" 
          }] 
         }] 
        }] 
       }] 
      }] 
     } 
    } 
} 

這是我的字符串,我需要的正則表達式,將得到所有ID與它的價值正則表達式:需要在PHP

例如:「ID」:「事件」,「ID」:「事件」, 「ID」:「KWD」,「ID」:「KWD」

意味着每一次出現,這樣我就可以更換或獨特的價值添加到ID

注: - 我不想做一個數組,不想用循環做,

找到可能用preg_matc替換h或任何其他正則表達式方法。

+4

請勿使用正則表達式來解析JSON。有[這個功能](http://php.net/manual/en/book.json.php)。 –

+3

使用'json_decode()'並將其轉換爲php數組。做任何你想要的,然後使用'json_encode()'轉換回json。順便說一句,我不是一個羽絨服的選民 –

+0

如果我想這樣做,我沒有問這個 –

回答

-1

我有我自己的答案:

preg_match_all( '/ 「ID」: 「(*)。」/ U',$ JSON,$比賽,PREG_PATTERN_ORDER);

print_r($ matches);死;

+2

你問一個問題,甚至沒有聽取人們在評論中的意見?見[#1](http://stackoverflow.com/questions/42648873/regex-needed-in-php#comment72423774_42648873)和[#2](http://stackoverflow.com/questions/42648873/regex-需要 - 在-PHP#comment72423805_42648873)。 **不要**使用正則表達式。 – Xorifelse

+0

大家都是通過把它轉換成數組來做這件事,我的動機只是在字符串中使用正則表達式。 –