0

So, I'm trying to display an associative array generated from a table in my database. These are the steps I'm trying to accomplish:

  1. Get result set from database that's already sorted in certain order based on a column(Ascending/Descending).
  2. Add/Transfer/Append each record/row in the result set to another array. While in the process of appending, check if the array we are adding records has a value(based on a different column) similar to the record we are currently adding.
  3. If the value we are checking for is already there, skip appending. If not, append the record and continue with the loop until the end of the result set.

This is the sample data I am working with, tbl_user_details: Sample data

As you can see, it has been sorted in descending order based on the user_pos column. If you look at the user_id column, we have duplicate values. What I am trying to achieve is adding each record to an array and skipping the duplicate values based on the user_id column.user_id = 4 and user_pos = 7. Next will be ada with user_id = 2 and user_pos = 6. The next record which we are not going add is woz with user_id = 2 and user_pos = 5 because we already have someone with user_id = 2. So, we skip to adding ghopper who has user_id = 3 and user_pos = 4... and so forth. I think this example explains the flow of data I need displayed.

I've tried some simple code to get this done which it's clearly not working. Here is the snippet:

$query3 = "SELECT * FROM `user_details` ORDER BY `user_details`.`user_pos` DESC";

$user_set = mysqli_query($connection, $query3);

  if (!$user_set) {
    die("Database query failed.".mysqli_error($connection));
  }

  $user_arr = array();

  while ($row = mysqli_fetch_assoc($user_set)) {

if (in_array($row["user_id"], $user_arr)) {
  continue; // skip if we already have this value in array
}

  $user_arr[] = $row;


}

The records that should be displayed are of aswartz, ada, ghopper and rasmusl. The rest are ignored. When I display the data in '$user_arr[]' using the foreach loop method, it still displays the whole data as it is on the table.

FabioBranch
  • 175
  • 4
  • 19
  • Can you add the dump of `$row` and `$user_arr` just to see the structure of the two arrays ? – teeyo Sep 22 '17 at 12:15

2 Answers2

1

It will become multi dimensional array when you add row to array so you cant check user_id in it. There are many other solutions to check unique record but One simplest solution is to assign user_id as key then check key for duplication

while ($row = mysqli_fetch_assoc($user_set)) {

if (isset($user_arr[$row["user_id"]])) {
  continue; // skip if we already have this value in array
}

  $user_arr[$row["user_id"]] = $row;


}

Note: If you want unique records only then you can use DISTINCT in query

B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • I just found your answer as easy to understand and more [faster](https://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset) Thanks for this answer I also leaned a lot. – Niklesh Raut Sep 22 '17 at 12:29
  • This worked!! With regards to using `DISTINCT`, how can one tell which record was picked? I also tried `GROUP BY` but the sorting part was not how I wanted it to be. – KipchirchirIan Sep 22 '17 at 12:41
  • @kipchirchirian if answer wored then accept.upvote answers. also to get help in query you need to show your data and expected output – B. Desai Sep 22 '17 at 12:58
  • @B.Desai I upvoted but with my rep it doesn't have any effect. The data given in my question above should suffice. – KipchirchirIan Sep 22 '17 at 15:21
0

Use another $tempArr which will contain only user_id to in in_array, In you code you are checking user_id against array or whole row which is user details not just against user_ids

$user_arr = array();
$tempArr = array();
while ($row = mysqli_fetch_assoc($user_set)) {
    if (in_array($row["user_id"], $tempArr)) {
        continue; // skip if we already have this value in array
    }
    $user_arr[] = $row;
    $tempArr[] = $row["user_id"];
}
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109