duanji5569 2018-05-31 14:51
浏览 70
已采纳

PHP数组长度

So this may be a bit long winded to explain, but I'll try to make it concise.

I have a db query that pulls in 3 different Tag IDs, 2 of them are associated to 4 Hub IDs, while 1 is associated with only 3.

I return all tags, and sort the results by ID (so all 4 results for tag 1 are grouped, all 4 of 2, and all 3 of 3) Like so:

Array
    (
        [0] => 40BD32751DF1
        [1] => 40BD32751DF1
        [2] => 40BD32751DF1
        [3] => 40BD32751DF1
        [4] => 10CEA9FD173A
        [5] => 10CEA9FD173A
        [6] => 10CEA9FD173A
        [7] => 10CEA9FD173A
        [8] => 10CEA9FCFE26
        [9] => 10CEA9FCFE26
        [10] => 10CEA9FCFE26
    )

Then I do a while loop and loop it for each Tag ID (3x). Inside that, I use array_keys on an array_column search to find the array indexes of each Tag ID, count how many results I have (4, 4, 3) I then take that row's data using the array key, and loop number to push that row of data into an array for later sorting:

while($currentTag = pg_fetch_assoc($tagList)) {
    $tkeys = array_keys(array_column($tagDataArray, 'devmac'), $currentTag['devmac']);
    $tempArray = array();
    for($k=0; $k < count($tkeys); $k++){
        array_push($tempArray, $tagDataArray[$tkeys[$k]]);
    }

//Then I sort that temporary array so one of the values in the row is the highest:

    foreach($tempArray as $sigkey => $sigrow) { 
        $sigsort[$sigkey] = $sigrow['devrssi']; 
    }
    array_multisort($sigsort, SORT_DESC, $tempArray);
    updateArticles($tempArray[0]);
}

Now the problem comes from that temporary array. I have 4 results for the first ID, 4 for the second, then 3 for the third, yet for the third ID, somehow I still get 4 items in the array even though I reinitialize it with each while loop pass (each ID). The fourth result from the second ID, ends up as the fourth result for the third ID.

How is this possible? I've been trying to figure this out for hours and I'm not making any headway. The $tkeys gives me 3 on the third ID, so the for loop runs 3 times, everything makes sense, till the array push where something just decides to stick around. I've even added a print_r to the tempArray and before it runs the third time, it's empty! I dont get where it keeps creeping back in.

Thanks.

  • 写回答

1条回答 默认 最新

  • doulu1968 2018-05-31 15:06
    关注

    Make sure that when you process data in subsequent loops, you remove all previous data...

    $sigsort= [];
    foreach($tempArray as $sigkey => $sigrow) { 
        $sigsort[$sigkey] = $sigrow['devrssi']; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)