1

I have a mysql DB with those column id , idUser , idCompany , idProfession for users table.

Now i would Select all those field and make an array that is build in this way : array[idUser] = array [id=> x , idCompany => xx , idProfession => xxx)

For example 1 user could work for more company and do different profession. So each array[idUser] Could have multiple array(id, idCompany,idProfession) and i would have 1 result for each idUser instead multiple result for the same idUser.

Now after i did the query i wrote this code but of course id doesnt do what i want becouse it replace always the same element with another one cause the while loop.

$data=array();
$sql_result="SELECT etc...........";

    while($rows = mysql_fetch_array($sql_result,MYSQL_BOTH)){
        $data[idUser] = array('idCompany' => $rows['idCompany'], 'profession' => $rows['idProfession'] ); 

I was thinking to do a for loop in the while , store the data in a tmp array.

But i had to stop becouse i didnt understand how to do that.

1 Answer 1

1

Does this do what you want?

$users=array();
$sql_result="SELECT * FROM users";

while($row = mysql_fetch_array($sql_result,MYSQL_BOTH)){
    $users[ $row['idUser'] ][] = array('idCompany' => $row['idCompany'], 'profession' => $row['idProfession'] ); 
}

This will populate the array like this:

array(
  idUser => array(
      array('idCompany', 'profession'),
      array('idCompany', 'profession'),
      etc..
     )
)
Sign up to request clarification or add additional context in comments.

1 Comment

No problem Jasper, happends to us all my friend!

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.