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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。