skip to Main Content

I have a problem with my code and already tried to fetch data from the database and put them into an array with a single quote. However, I still don’t get the desired output.

phpMyAdmin result after run SQL

My code

$sql = "SELECT DISTINCT S.SKILL_NAME ,GROUP_CONCAT(SK.SKILLSET_NAME) AS SKILLSET_ALL
        FROM SKILL S
        LEFT JOIN SKILLSET SK ON S.SKILL_ID = SK.SKILL_ID
        GROUP BY S.SKILL_ID, S.SKILL_NAME";

$result = mysqli_query($conn,$sql);
$skillset = [];
$skillname = [];
$count = 0;
$checkrows=mysqli_num_rows($result);

    

if ($checkrows > 0)
{
    while($row = mysqli_fetch_array($result))
    {
        $skillname[] = $row['SKILL_NAME'];
        $skillset[] = $row['SKILLSET_ALL'];
        $count++;
    }

}
    $keys = array_keys($skillname);
    for($i = 0; $i < count($skillname); $i++) {
        $objects =
        [
            $skillname[$i] . " => '". $skillset[$i] . "'",
        ]; 
        print_r($objects);
    }

What I get after print_r($objects)

Array ( [0] => ANDROID STUDIO => 'Array,Array,Array,cs230,cs230' ) Array ( [0] => C# => 'Java,Eclipse,Java,Eclipse,C#' )

What I need

'ANDROID STUDIO' => ['cs230', 'cs230', 'Array', 'Array', 'Array'],
'C#' => ['Java', 'Eclipse', 'Java', 'Eclipse', 'C#],

Thank you.

2

Answers


  1. I think you can put the single quote right before and after $skillset.

    $keys = array_keys($skillname);
    
    for($i = 0; $i < count($skillname); $i++) {
        $objects =
        [
            $skillname[$i] . " => '". $skillset[$i] . "'",
        ]; 
        print_r($objects);
    }
    
    Login or Signup to reply.
  2. As I looked at your SQL table, look like you got $row['SKILLSET_ALL'] as string not as array

    Change it to array using explode:

    $skillset[] = explode(',', $row['SKILLSET_ALL'])
    

    perhaps you got your desired output

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search