2017-08-31 20 views
0

总之,我正在从category_id,product_id和活动活动标签下载。这个想法就像所有分配给特定category_id的产品的值为0一样,我显示回显为“空”,否则显示“产品包含”,但foreach循环会通过产品ID并显示空的+ category_id一次,通过通过$状态并显示如此多的回声,有多少记录处于活动状态。复制foreach中的if结果

查询是好的 - 我知道我不应该对数组执行逻辑操作,但是数据库的结构使我没有输出。

$result = array(); 

      foreach ($products as $detail) { 

       $cid = $detail['category_id']; 
       $pid = $detail['product_id']; 
       $status = $detail['active']; 
       $result[$cid][$pid] = $status; 

       $multiplier = $result[$cid][$pid]; 
       $counter = count($result[$cid]); 
       $multiplier2 = 1; 


       $multiplier = $multiplier * $multiplier2; 
       echo '$status: ' .$multiplier. " | "; 

       if ($multiplier == 0) { 
        echo '$cid: '.$cid.' | empty <br />'; 
       } else { 
        echo '$cid: '.$cid.' | products contains <br />'; 
       } 

      } 

重复的原因,但我不知道如何更改代码在这段代码

Array 
(
    [0] => Array 
     (
      [product_id] => 1 
      [0] => 1 
      [category_id] => 221 
      [1] => 221 
      [active] => 0 
      [2] => 0 
     ) 

    [1] => Array 
     (
      [product_id] => 2 
      [0] => 2 
      [category_id] => 221 
      [1] => 221 
      [active] => 0 
      [2] => 0 
     ) 
) 

$结果阵列

Array 
(
    [221] => Array 
     (
      [1] => 0 
      [2] => 0 
     ) 
) 

@EDIT

目前,该功能返回像这样的东西

enter image description here

而基于此阵,它应该是这样的 - 你可以看到1437已经=> 1,因此该类别不为空

enter image description here

+1

你能否更好地解释你在做什么以及你想达到什么目的?也许包括当前输出的数据以及它应该是什么样子。 – IsThisJavascript

+0

我添加了一些信息,够了吗? – sauero

+0

您能否提供成为您当前输出的样本输入数组? – mickmackusa

回答

0

这听起来像你正在尝试从分散的列表中分离什么是空的(它听起来像你可能能够做一个更好的查询)。看看这个迭代是否有助于过滤。

function getCategoryActivity($products) 
    { 
     $store = array(); 
     foreach($products as $detail) { 
      $cid = $detail['category_id']; 
      $pid = $detail['product_id']; 
      # If the cid is not already stored, store it 
      if(!isset($store[$cid])) 
       $store[$cid] = $detail['active']; 
      else { 
       # If it's already set to active, ignore it 
       if(!empty($store[$cid])) 
        continue; 
       else 
        # Store it if not currently active 
        $store[$cid] = $detail['active']; 
      } 
     } 
     # Send back the filtered array 
     return $store; 
    } 
# Fetch the filtered array 
$sorted = getCategoryActivity($products); 

$sorted应该给你现在的类别,在他们,而不是在其中有活动的列表。您可以使用array_map()或另一个foreach()echo再次迭代一次。我不知道你想用这个乘法器部分做什么,所以我忽略了它。