2

I am a designer working on a code igniter project and I am stuck on this problem!

I have this array

Array
(
     [Science] => 7
[War] => 5
[Mystery] => 2
[Preview Only] => 2
[Conspiracy,News ] => 2
[Drugs,Educational,Lifestyle,Society] => 1
[Educational,Nature,Science,Space] => 1
[Religion,Society] => 1
[Music] => 1
[Movies,Music] => 1
[Lifestyle] => 1
[Countries,Educational,History,War] => 1
[Educational,Society] => 1
[Science,Space,Technology] => 1
[Economics,Educational,Lifestyle,Society] => 1
[Society] => 1
[Biology,Conference,Educational] => 1
[Human Rights,Society] => 1
[Art and Artists,Biographies,Music] => 1
[Biology,Educational,Science] => 1
[Activist,Biology,Science,Society] => 1
[Countries,Drugs,War] => 1
[Drugs,War] => 1
[Technology,War] => 1
[History,War] => 1
[Biology,Educational,Environment,Nature,Science] => 1
[Science,Technology] => 1
[Educational,Science,Space] => 1
[Educational,History,Human Rights,War] => 1
[Environment] => 1
[Biographies] => 1
[Evolution,Science] => 1
[Art and Artists] => 1
)

As you can see there it is already ordered by a number. What I would like to achieve is removing any duplicate categories from the array and adding a +1 to each category, leaving me with this result:

Array
(
[Science] => 14
[War] => 11
[Mystery] => 2
[Preview Only] => 2
[Conspiracy] => 2
[Society] => 4
[Nature] => 1
[space] => 1
[Religion] => 1
[Music] => 2
[Movies] => 1
[Lifestyle] => 1
[Countries] => 1
[Educational] => 12
[Space] => 1
[Lifestyle] => 1
[Society] => 3
[Biology] => 3
[Economics] => 1
[Science] => 1
[Activist] => 1
[Countries] => 1
[Drugs] => 3
[Technology] => 2
[History] => 1
[Biology] => 1
[Technology] => 1
[Space] => 1
[History] => 1
[Human Rights] => 
[Environment] => 1
[Biographies] => 2
    [Evolution] => 1
    [Art and Artists] => 1
)   

The examples don't perfectly match but you can see what I am trying to achieve. THANKS EVERYONE!!!

4

3 回答 3

1

You can use array_keys() to get the key of the first array, explode() to cut the commas, and then, if explode gave more than one element, check if that element exists or not, and increment by one, or create a new.

This a first version, not even tested:

   $array1 = array(); // The array you show
   $array2 = array(); // The resulting array

   $keys = array_keys($array1);
   foreach($keys as $key)
   {
        $chunks = explode(',', $key);
        foreach($chunks as $chunk)
        {
             if(isset($array2[$chunk]))
                 $array[$chunk] += $array1[$key];
             else
                 $array[$chunk] = $array1[$key];
        }
    }
于 2013-08-11T11:44:38.850 回答
1
function do_the_magic( $array ){
 $result = array();
 foreach ($array as $k=>$value){
  foreach (explode(',',$k) as $category){
   $result[$category] += $value;
  }
 }
 return $result;
}

example:

$a['ala,ma']=1;
$a['ala']=2;
print_r(do_the_magic($a));

results in

Array ( 
 [ala] => 3 
 [ma] => 1 
)
于 2013-08-11T11:46:29.767 回答
1

尝试这样的事情:

$result = array();
foreach($data as $str_keys => $value) {
    $keys = explode(',', $str_keys);
    foreach($keys as $key) {
        if(isset($result[$key])) {
            $result[$key] += $value;
        } else {
            $result[$key] = $value;
        }
    }
}

print_r($result);
于 2013-08-11T11:48:32.610 回答