计算数组中的元素

时间:2015-12-07 05:48:23

标签: php arrays json

我想计算数组中喜欢和不同的出现次数 $content{"userid":"1","like":"1"}{"userid":"1","unlike":"1"} 我想和表中的记录一样计算。 对于我的桌子,我需要显示like_count=3unlike_count=1。 现在显示like_count=5unlike_count=5

enter image description here

$like_count=0;
$unlike_count=0;
 while($like_fet=mysql_fetch_assoc($query))
 {
  $content = $like_fet['CONTENT_VALUE'];
  $datas = json_decode($content);
  foreach($datas as $item)
  {
   $like=$item['like'];
   if($like=='1')
    {
    echo "like";   
    $like_count=$like_count+1;  
    }
   $unlike=$item['unlike'];
   if($unlike=='1')
    {
    echo "unlike";
    $unlike_count=$unlike_count+1;  
    }
  }

 }
  echo "like_count=".$like_count;
 echo "unlike_count=".$unlike_count;

3 个答案:

答案 0 :(得分:0)

JSON_decode返回一个对象;不是数组,除非添加第二个参数。

  

当为TRUE时,返回的对象将被转换为关联数组。

所以你可以这样做:

$strings = array('{"userid" :"1","like":"1"}', '{"userid" :"1","like":"1"}', '{"userid" :"1","unlike":"1"}');
$likes = 0;
$unlikes = 0;
foreach($strings as $string){
    $array = json_decode($string);
    if($array->like != '') {
        $likes++;
    }
    if($array->unlike != '') {
        $unlikes++;
    }
}
echo $likes;
echo $unlikes;

输出:

21

$strings = array('{"userid" :"1","like":"1"}', '{"userid" :"1","like":"1"}', '{"userid" :"1","unlike":"1"}');
$likes = 0;
$unlikes = 0;
foreach($strings as $string){
    $array = json_decode($string, 1);
    if(!empty($array['like'])) {
        $likes++;
    }
    if(!empty($array['unlike'])) {
        $unlikes++;
    }
}
echo $likes;
echo $unlikes;

演示:https://eval.in/481191

所以你的代码应该是这样的:

$like_count=0;
$unlike_count=0;
//while($like_fet=mysql_fetch_assoc($query))
//{
    $content = '{"userid" :"1","like":"1"}'; //$like_fet['CONTENT_VALUE'];
    $datas = json_decode($content, 1);
    foreach($datas as $key => $item) {
        if($key == 'like')
        {
            echo "like";
            $like_count++;
        }
        if($key == 'unlike')
        {
            echo "unlike";
            $unlike_count++;  
        }
    }
//}
echo "like_count=".$like_count;
echo "unlike_count=".$unlike_count;

输出:

likelike_count=1unlike_count=0

答案 1 :(得分:0)

我已经尝试过没有Jsondecode的要求了。它可能会帮助你。我解释了正在解释的代码:让我知道输出

<?php
$like_count=0;
$unlike_count=0;
 while($like_fet=mysql_fetch_assoc($query))
 {
    $content = $like_fet['CONTENT_VALUE'];
 //$content='{"user_id:"1","unlike":"1"}';
  $str=trim($content,'{}');
  $a=explode(',',$str);
  $a[1]=trim($a[1]);
  $variable='"unlike":"1"';
$com=strcmp($a[1],$variable);

if($com==0){
    $unlike_count=$unlike_count+1;
}
else{
    $like_count= $like_count+1;
}
}
 echo "like_count=".$like_count;
 echo "unlike_count=".$unlike_count;

?>

说明:

<?php
$str='{"user_id:"1","unlike":"1"}';//assigned $like_fet['content']
$str=trim($str,'{}');// removed {}
$a=explode(',',$str);// splits the string with , which results an array
$unlike_count=0;
$like_count=0;
print_r($a);// print see 
echo"<br>"; // line break
echo $a[1]=trim($a[1]);// removes free space
$variable='"unlike":"1"';// assigning a value as in in the db 
$com=strcmp($a[1],$variable);// compare and check returs 0 if match
if($com==0){
    echo "good";
    echo $unlike_count=$unlike_count+1;
}
else{
    echo"bad";
    echo $like_count= $like_count+1;
}
?>

答案 2 :(得分:0)

我通过代码解决了。它工作得很好。

$like_count=0;
$unlike_count=0;
while($like_fet=mysql_fetch_assoc($query)) {
$json = json_decode($like_fet['CONTENT_VALUE'], true);
if ( isset($json['like']) ) {
    $like_count++;
}
else if ( isset($json['unlike']) ) {
    $unlike_count++;
}

}