0

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.

5
  • I also want to mention that there was a problem with my last post, so I apologize if it looks familiar! Commented May 31, 2018 at 14:52
  • It isn't clear to me from your explanation what your end goal is? What is the desired output? Commented May 31, 2018 at 14:57
  • Can you please add some var_dump output somewhere in your question? Moreover can you please explain what you want to achieve? Commented May 31, 2018 at 14:59
  • What did you get and what is the desired output please? Commented May 31, 2018 at 15:01
  • Of course! Give me a moment. I'll write up a detailed var_dump page and post the pastebin? Commented May 31, 2018 at 15:04

1 Answer 1

1

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']; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

SUCH A SIMPLE ANSWER... Thank you so much! Omg, I feel so dumb
I like simple answers. Everyone makes mistakes, just learn from them and move on :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.