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
I think you can put the single quote right before and after
$skillset
.As I looked at your SQL table, look like you got
$row['SKILLSET_ALL']
as string not as arrayChange it to array using
explode
:perhaps you got your desired output