1

I have a MySQL table:

-------------------
|type_id|type_name| 
-------------------
|  22   | Toyota  |
|  22   | Mazda   |
|  23   | Volvo   |
|  23   |  Man    |
|  25   | Scania  |
|  25   | Iveco   |
|  25   | Fiat    |
-------------------

which is created dynamically from user input. I want to create an array from the table using PHP like this:

array(
    '22' => array('Toyota', 'Mazda'),
    '23' => array('Volvo', 'Man'),
    '25' => array('Scania', 'Iveco','Fiat'),
);

where array id will be type_id and elements will be type_name's of the type_id.I tried this:

$results = array();
    while($line = mysqli_fetch_array($result, MYSQL_ASSOC))
    {
    $results[] = $line;
    }   
    print_r($results);

But I am getting:

Array ( 
    [0] => Array ( 
            [type_id] => 22 
            [type_name] => Toyota
        ) 
    [1] => Array ( 
            [type_name] => 22 
            [type_name] => Mazda
        )
    ...so on

Please help!

9
  • Did you try anything? I suggest to use loops... Commented Jul 30, 2017 at 7:48
  • did you tried anything,paste here. We can't do full code for you. Please show your effort Commented Jul 30, 2017 at 7:50
  • @BibanCS check my answer. Commented Jul 30, 2017 at 8:05
  • Is the order of the makes within each group important? If so, you need to include some way of defining that order. Usually this is done through the mechanism of a PRIMARY KEY Commented Jul 30, 2017 at 8:07
  • SELECT type_id, GROUP_CONCAT(type_name) FROM TABLENAME GROUP BY type_id this query will give you exactly what you are looking for. PHP processing is not needed. Commented Jul 30, 2017 at 8:14

3 Answers 3

1

You need to change your code a bit and it will work fine:-

$results = array(); 
while($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { 
  $results[$line['type_id']][] = $line['Type_name'];  //check change here
} 
print_r($results);

Note:- check your column-names and correct them if any mistake is there.

Sign up to request clarification or add additional context in comments.

1 Comment

@BibanCS glad to help you :):)
1

Please replace your db name and table name in the code-

$conn = mysqli_connect("localhost","root","","dbname");
$query ="select * from table";
$exe = mysqli_query($conn,$query);

$input = array();
$type = array();
while ($result = mysqli_fetch_array($exe)) 
{
    if(array_key_exists($result['type_id'], $input))
    {
        $type[] = $result['type_name'];
    }
    else
    {
        $type = array($result['type_name']);
    }
    $input[$result['type_id']] = $type;
}
echo "<pre>";
print_r($input);

3 Comments

is there any mistake on above code ? i would like to know about it. you have several way to do this. this is one of the way. if it is wrong or it doesn't work please let me know.
OP don't need to change his complete code like you.Just a small change in his fetching data will lead him to desired output as he said that he is able to fetch record fine.BTW not a down-voter
Thanks mate. In the starting when i see his post he didn't have the code ...later he uploaded it so i couldn't able to see his code....
0

select type_id and Type_name columns from table and in foreach loop do this:

$output = array();
foreach ($result as $item){
    $output[$item['type_id']][] = $item['Type_name'];
}

Comments

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.