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.

link|improve this question

77% accept rate
feedback

1 Answer

up vote 1 down vote accepted

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..
     )
)
link|improve this answer
Thanks , i feel just an idiot :D Was simple! Thanks – Jasper Nov 18 '11 at 18:46
No problem Jasper, happends to us all my friend! – Jason Brumwell Nov 18 '11 at 20:48
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.