请参阅下表:
id title fields
1 Dog [{"id":"11","value":"50"},{"id":"19","value":"100"},{"id":"22","value":"150"}]
2 Cat [{"id":"19","value":"50"},{"id":"22","value":"100"},{"id":"37","value":"150"}]
3 Bird [{"id":"14","value":"50"},{"id":"19","value":"100"},{"id":"22","value":"150"}]
4 Mouse [{"id":"16","value":"50"},{"id":"22","value":"100"},{"id":"34","value":"150"}]
5 Horse [{"id":"6","value":"50"},{"id":"22","value":"100"},{"id":"47","value":"150"}]
我想回显所有等于id 22的值的总和 我是php和mysql的新手,非常感谢您的帮助,所以请清楚地向我解释一下代码。
这是我发现的,但不起作用:
<?php
if ($fields->id == 22) {
$result += $fields['value'];
}
return $result;
?>
请帮助....谢谢
答案 0 :(得分:2)
看起来$ fields是json数据。如果是,那么
试试这个:
$data = json_decode($fields);
foreach($data as $element) {
if ($fields->id == 22) {
$result += intval($fields->value);
}
}
return $result;