html - Get total number of name field from JSON within PHP -
rather new using json php, bit of lastly resort having searched on net quite bit already. have illustration ison file below, wish able echo total number of 'name:' fields within eg. 4 in illustration below.
question: how go doing this?
[ { "age": "22", "name": "dave" }, { "age": "21", "name": "alan" }, { "age": "19", "name": "luke" }, { "age": "30", "name": "nina" } ]
if there name in each array just:
echo count(json_decode($json, true)); if name may or not nowadays in each array then:
php >= 5.5.0:
echo count(array_column(json_decode($json, true), 'name')); php < 5.5.0:
$count = 0; foreach(json_decode($json, true) $k => $v) { isset($v['name']) ? $count++ : $count; } echo $count; php html json
No comments:
Post a Comment