0

I store numbers in an array in the following way:
467:0
where 467 is the number and 0 is the number of occurances.

What am doing is I grab a bunch of numbers from the db and then I look for numbers that appear more then once, if they do incredement the number of occurances.

$numberCount = explode(':', $this->_presentationArr);

    if(in_array($numberprefix  . ':' . preg_match('/^\d+$/', $numberCount[1]), $this->_presentationArr))
    {

        $pos = array_search($numberprefix  . ':' . preg_match('/^\d+$/', $numberCount[1]), $this->_presentationArr);
        $tmpArr = explode(':', $this->_presentationArr[$pos]);
        $tmpArr[1]++; # this does not work


    }


The $tmpArr[1] is the number that needs to be incredementet for each match. Any ideas?

4

1 回答 1

0

我认为你把事情复杂化了。php 数组与 C 中的数组有点不同,它们的行为更像一个地图。这意味着您可以只插入键而不用担心前一个键是否真的存在。这意味着您可以轻松快速地创建频率图,如下所示:

$occurrencearray = array()
// this next line is pseudocode
while ($fetched = fetchnumber()) {
    $occurrencearray[$fetched]++;
}
// once all numbers loaded you can iterate over your occurrence map
foreach ($occurrencearray as $key => $value) {
    printf("Number %d occurs %d times",$key,$value);
    // or of course reconvert it to "key:value" strings for storing
}
于 2013-07-15T08:51:14.167 回答