2015-12-07 92 views
-1

我想計算數組中出現的像和不像數。
$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; 
+0

你面臨的問題是什麼? – Andrew

+0

嘗試檢查是否(isset($ item ['like'])&& $ item ['like'] == 1){$ like_count ++}。同樣檢查不同。 – Ravneet

+0

現在我抓錯了值like_count = 5和different_count = 5。 – user3386779

回答

0

我的工作完美的code.Its解決。

$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++; 
} 

} 
+0

因此,你用我的答案第二個參數? – chris85

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 
+0

這兩個返回喜歡:0不像:0 – user3386779

+0

@ user3386779更新與如何在您的代碼中使用.. – chris85

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; 
} 
?>