-1

我得到了这个 json 格式的表单生成器插件,希望有人能帮忙我需要计算某些字段出现的次数,这样我就可以为他们创建一个数据库

[ { "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 或 nodejs 中的任何解决方案,因为我对 Json 很陌生

!=================================================== ============================!大家好,感谢您的及时回复,我很感激并很荣幸有你,但它并没有真正解决挑战,也许我不够清楚,所以这是程序逻辑。对于给定的 json 数据,我希望以某种方式对其进行排序,以便我可以获得复选框的图块及其值,从而为用户创建数据库条目,例如,对于带有复选框的数组,获取标题和值的数量(值)所以我可以有类似说 checkbox1 标题值 = 6 checkbox2 标题值 = 5

4

4 回答 4

1

您示例中的 JSON 对象是一个数组。您可以使用以下代码来计算所有值:

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

或者您可以使用以下方法到达一个对象:

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

当然你需要使用

json_decode($json_string,true)

将您的 json 对象转换为 PHP 数组。

于 2013-05-13T14:16:31.587 回答
0

你永远不会直接处理“json objects”。JSON 只是一个恰好代表 javascript 样式数据结构的文本字符串。你总是处理 NATIVE 数据结构,所以

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

是您在 PHP 中所做的 - 转换为原生 PHP 数组/对象,然后依靠它。

至于你应该指望什么,一旦你解码回原生 PHP,你可以检查实际结构

var_dump($array);
于 2013-05-13T14:13:38.467 回答
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);
于 2013-05-13T14:16:27.427 回答
0

我认为通过循环你会得到你想要的东西(但我不能 100% 确定我是否理解正确)。

<?php 
$json   = '[ { "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" } } } ]';

// first convert the json string to an array (as the others already said)
$array  = json_decode($json);   
$boxes  = array();              
foreach ($array as $obj) 
{
    if ($obj->cssClass == "checkbox")
    {
        $boxes[] = array(
            "title" => $obj->title,
            "count_values" => count( (array)$obj->values )  // cast the object as an array to count the indexes
        );
    }
}      
?>

这将为您提供一个新数组,其中仅包含带有标题的复选框和值的数量。

/*
Array
(
    [0] => Array
        (
            [title] => hello save
            [count_values] => 6
        )

    [1] => Array
        (
            [title] => whats up save
            [count_values] => 5
        )

)
*/
于 2018-05-13T21:56:15.407 回答