dongshi2588 2013-05-13 14:11
浏览 46

在php中计算JSON多维数组

i got this json format form form-builder plugin, in hope someone could help out i need to count the number of times certain fields occur so i can create a database for them

[ { "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"
          }
      }
  }
]

i want to be able to count the number of cssClass checkbox and the corresponding values value to help create a database table for insert in mysql or mongodb i will like it in a format like this

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

i get

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

i will appreciate any solution in php or nodejs sice am quite new to Json

!============================================================================! Hello guys, thanks for your timely response, i appreciate and feel very privileged to have u guy but it didn't really solve the challenge maybe i was not clear enough so this is the program logic. for the given json data i will like to sort it in a way that i can get the tile of the checkbox and its values which to create database entries for the user e.g for array with checkbox get title and number of values (value) so i can have something like say checkbox1 title values = 6 checkbox2 title values = 5

  • 写回答

4条回答 默认 最新

  • dongshuming7131 2013-05-13 14:13
    关注

    You never deal with "json objects" directly. JSON is just a text string that happens to represent a javascript-style data structure. You always deal with NATIVE data structures, so

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

    Is what you'd do in PHP - convert to a native PHP array/object, then do your counting on that.

    And as for what you should be counting on, once you've decoded back to native PHP, you can examine the actual structure with

    var_dump($array);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog