3 回答
TA贡献1848条经验 获得超10个赞
您尝试计算的项目位于单个字符串中。首先,您必须将字符串分成多个项目,然后您才能对其进行计数。
获取json并将其变成一个php数组
$jsonData = '{
"posts_id_en": "149968, 149939, 149883, 149877, 149876, 149847, 154303",
"posts_id_fr": "149974,149953,149926, 149920, 149901",
"episode_status": "3"
}';
$data = json_decode($jsonData, true);
然后用分隔符“,”分割字符串
$items = explode(", ", $data['posts_id_en']);
然后数
echo count($items);
TA贡献1851条经验 获得超3个赞
<?php
$json = '{
"posts_id_en": "149968, 149939, 149883, 149877, 149876, 149847, 154303",
"posts_id_fr": "149974,149953,149926, 149920, 149901",
"episode_status": "3"
}';
$decoded = json_decode($json, true);
$post_id = $decoded['posts_id_en'];
$resultList = [];
foreach($decoded as $key => $entry) {
$everyNumberAsArray = explode(',', $entry);
$count = count($everyNumberAsArray);
$resultList[$key] = $count;
}
var_export($resultList);
给出输出:
array (
'posts_id_en' => 7,
'posts_id_fr' => 5,
'episode_status' => 1,
)
要获得特定值,您可以通过以下方式使用它们:
echo $resultList['posts_id_en'] . '<br>' . PHP_EOL;
这给你:
7
TA贡献1864条经验 获得超2个赞
一种简单的方法是,我们首先json_decode验证所需属性中的数字,并计算匹配项:
$str = '{
"posts_id_en": "149968, 149939, 149883, 149877, 149876, 149847, 154303",
"posts_id_fr": "149974,149953,149926, 149920, 149901",
"episode_status": "3"
}';
$str_array = json_decode($str, true);
preg_match_all('/(\d+)/s', $str_array["posts_id_en"], $matches);
echo sizeof($matches[0]);
输出
7
- 3 回答
- 0 关注
- 414 浏览
添加回答
举报