2013-05-13 85 views
-1

我得到這個JSON格式的表格形式建設者插件,在希望有人可以幫幫忙,我需要計算髮生的某些字段,以便次數我能爲他們計數JSON multidimendional陣列

[ { "cssClass" : "checkbox", 
    "required" : "false", 
    "title" : "hello save", 
    "values" : { "2" : { "baseline" : "false", 
      "value" : "save" 
      }, 
     "3" : { "baseline" : "false", 
      "value" : " save 2" 
      }, 
     "4" : { "baseline" : "false", 
      "value" : "save 3" 
      }, 
     "5" : { "baseline" : "false", 
      "value" : "save 4" 
      }, 
     "6" : { "baseline" : "false", 
      "value" : "save 5 " 
      }, 
     "7" : { "baseline" : "false", 
      "value" : "Save 6" 
      } 
     } 
    }, 
    { "cssClass" : "textarea", 
    "required" : "false", 
    "values" : "you did not say hello properly" 
    }, 
    { "cssClass" : "checkbox", 
    "required" : "false", 
    "title" : "whats up save", 
    "values" : { "2" : { "baseline" : "false", 
      "value" : "i got your back" 
      }, 
     "3" : { "baseline" : "false", 
      "value" : "i got your back 2" 
      }, 
     "4" : { "baseline" : "false", 
      "value" : "i got your back 3" 
      }, 
     "5" : { "baseline" : "false", 
      "value" : "1 got your back 4" 
      }, 
     "6" : { "baseline" : "false", 
      "value" : "i got your back 5" 
      } 
     } 
    } 
] 
創建數據庫

我希望能夠算的CssClass複選框的數量和相應的值值,以幫助在MySQL創建插入數據庫表或MongoDB的 我會喜歡它在這樣

count($data[cssclass]) 
count($data[cssClass][values]) 

我得到的格式

Notice: Undefined index: cssClass in C:\wamp\www\callme\repo\index.php on line 45 

我會很高興在PHP的任何解決方案或SICE的NodeJS很新成JSON

!======================== ================================================== ==! 大家好,非常感謝您的及時回覆,我很欣賞並感到非常榮幸擁有你的傢伙,但它並沒有真正解決這個挑戰,也許我還不夠清楚,所以這是程序邏輯。對於給定的json數據,我會按照一種方式對它進行排序,以便我可以獲取複選框的瓦片和爲用戶創建數據庫條目的值,例如 用於複選框的數組獲取標題和值的數量(值)我可以有像說 複選框1標題值= 6 複選框2標題值= 5

回答

0

你從來沒有直接處理「json對象」。 JSON只是恰好代表JavaScript樣式數據結構的文本字符串。你總是處理本地數據結構,所以

$array = json_decode($your_json); 
$count = count($array['cssclass']); 

是你在PHP做什麼 - 轉換爲原生PHP數組/對象,然後做你的票。

至於什麼,你應該指望,一旦你解碼回本地PHP,你可以檢查與

var_dump($array); 
+0

你好,謝謝你的迴應,但它並沒有真正解決這個挑戰,也許我還不夠清楚,所以這是程序邏輯。 – user2377965 2013-05-14 14:12:48

0

實際結構這應該讓你有:

<?php 

$json = '[{"cssClass":"checkbox","required":"false","title":"hello save","values":{"2":{"value":"save","baseline":"false"},"3":{"value":" save 2","baseline":"false"},"4":{"value":"save 3","baseline":"false"},"5":{"value":"save 4","baseline":"false"},"6":{"value":"save 5 ","baseline":"false"},"7":{"value":"Save 6","baseline":"false"}}},{"cssClass":"textarea","required":"false","values":"you did not say hello properly"},{"cssClass":"checkbox","required":"false","title":"whats up save","values":{"2":{"value":"i got your back","baseline":"false"},"3":{"value":"i got your back 2","baseline":"false"},"4":{"value":"i got your back 3","baseline":"false"},"5":{"value":"1 got your back 4","baseline":"false"},"6":{"value":"i got your back 5","baseline":"false"}}}]'; 
$obj = json_decode($json, true); 

$cssClasses = array_filter($obj, function($el){ return $el["cssClass"] == "checkbox" }); 

print_r($cssClasses); 
1

您示例中的JSON對象是一個Array。您可以使用下面的代碼來計算所有的值:

$total = 0; 
foreach($data as $row){ 
    $total += count($row['values']); 
} 

,或者您可以使用以下達到一個目標:

count($data[0]['values']); 

當然,你需要使用

json_decode($json_string,true) 

將您的json對象轉換爲PHP數組。